如何使用Java直接从Internet读取文本文件?
我正试图从在线文本文件中读取一些单词 我试过这样做如何使用Java直接从Internet读取文本文件?,java,file,text-files,java.util.scanner,Java,File,Text Files,Java.util.scanner,我正试图从在线文本文件中读取一些单词 我试过这样做 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 作为输出,我只想得到所有的单词 我知道他们以前教过我这个,但我现在不记得怎么做了,非常感谢任何帮
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
作为输出,我只想得到所有的单词
我知道他们以前教过我这个,但我现在不记得怎么做了,非常感谢任何帮助。试试这样的方法
URL u = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
InputStream in = u.openStream();
然后将其用作任何普通的旧输入流使用
URL
而不是文件
进行任何不在本地计算机上的访问
URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");
Scanner s = new Scanner(url.openStream());
实际上,URL更普遍地有用,也适用于本地访问(使用文件:
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.
}
对于旧学校输入流,请使用以下代码:
InputStream in = new URL("http://google.com/").openConnection().getInputStream();
我用下面的方法处理图像,你应该可以用类似的步骤处理文本
// 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();
使用:
真正对我有用的东西:(来源:oracle文档“阅读url”)
使用此代码将Internet资源读入
字符串中
:
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();
}
这是基于。或者,您可以使用对象:
URL=新URL(“http://www.puzzlers.org/pub/wordlists/pocket.txt");
列表行=Resources.readLines(url,Charsets.UTF_8);
lines.forEach(System.out::println);
我在线程“main”java.lang中遇到异常。错误:未解决的编译问题:未处理的异常类型MalformDurException未处理的异常类型IOException将其包装在try/catch块中并捕获这两个异常。很抱歉,我迷路了,这难道不应该很简单,并且可以用2或3行代码来完成吗?例如,Explorer see。看起来您的服务器配置为不允许下载此文件。我理解。非常感谢。要求主机关闭此安全开关是否明智?我收到一个错误:类型test的方法URL(URL)未定义,或者用openStream()替换openConnection()。getInputStream()为openStream();)你需要“新的”-我猜你是在省略“URL(…)”前面的“新的”,可能是
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();
}
}
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();
}
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);