[英]How to read a text file directly from Internet using Java?
我正在尝试从在线文本文件中读取一些单词。
我试着做这样的事情
File file = new File("http://www.puzzlers.org/pub/wordlists/pocket.txt"); Scanner scan = new Scanner(file);
但它没有用,我得到
http://www.puzzlers.org/pub/wordlists/pocket.txt
作为 output,我只想得到所有的话。
我知道他们以前教过我这个,但我现在不记得到底该怎么做,非常感谢任何帮助。
对于本地计算机上没有的任何访问,请使用URL
而不是File
。
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(url.openStream());
实际上,URL更普遍有用,也适用于本地访问(使用file:
URL),jar文件以及可以某种方式检索的所有内容。
上面的方法解释了您的平台默认编码中的文件。 如果您想使用服务器指示的编码,则必须使用URLConnection并解析它的内容类型, 如此问题的答案中所示 。
关于您的错误,请确保您的文件编译没有任何错误 - 您需要处理异常。 单击IDE提供的红色消息,它应该向您显示如何修复它的建议。 不要启动无法编译的程序(即使IDE允许这样做)。
这里有一些示例异常处理:
try {
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(url.openStream());
// read from your scanner
}
catch(IOException ex) {
// there was some connection problem, or the file did not exist on the server,
// or your URL was not in the right format.
// think about what to do now, and put it here.
ex.printStackTrace(); // for now, simply output it.
}
尝试这样的事情
URL u = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
InputStream in = u.openStream();
然后将其用作任何普通的旧输入流
什么对我有用:(来源:oracle文档“阅读网址”)
import java.net.*;
import java.io.*;
public class UrlTextfile {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://yoursite.com/yourfile.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public static String readURLToString(String url) throws IOException
{
try (InputStream inputStream = new URL(url).openStream())
{
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
}
}
我通过以下方式为图像做到了这一点,你应该能够使用类似的步骤为文本做到这一点。
// folder & name of image on PC
File fileObj = new File("C:\\Displayable\\imgcopy.jpg");
Boolean testB = fileObj.createNewFile();
System.out.println("Test this file eeeeeeeeeeeeeeeeeeee "+testB);
// image on server
URL url = new URL("http://localhost:8181/POPTEST2/imgone.jpg");
InputStream webIS = url.openStream();
FileOutputStream fo = new FileOutputStream(fileObj);
int c = 0;
do {
c = webIS.read();
System.out.println("==============> " + c);
if (c !=-1) {
fo.write((byte) c);
}
} while(c != -1);
webIS.close();
fo.close();
对于旧学校输入流,请使用以下代码:
InputStream in = new URL("http://google.com/").openConnection().getInputStream();
使用此代码将Internet资源读入String
:
public static String readToString(String targetURL) throws IOException
{
URL url = new URL(targetURL);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(url.openStream()));
StringBuilder stringBuilder = new StringBuilder();
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null)
{
stringBuilder.append(inputLine);
stringBuilder.append(System.lineSeparator());
}
bufferedReader.close();
return stringBuilder.toString().trim();
}
这是基于这里 。
或者,您可以使用Guava的Resources对象:
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
List<String> lines = Resources.readLines(url, Charsets.UTF_8);
lines.forEach(System.out::println);
现在不推荐使用更正的方法。 它提供选项private WeakReference<MyActivity> activityReference;
这里的解决方案会很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.