Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java url连接问题_Java_Url - Fatal编程技术网

Java url连接问题

Java url连接问题,java,url,Java,Url,我正在读取包含以下代码的URL: URL myURL = new URL("htpp://path_to_my_file"); try { BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream())); while (reader.ready()) { String line = reader.readLine(); ...

我正在读取包含以下代码的URL:

URL myURL = new URL("htpp://path_to_my_file");
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(myURL.openStream()));

    while (reader.ready()) {
                String line = reader.readLine();
    ...
    }
} catch (IOException e) {
        throw new RuntimeException("Parsing of file failed: " + myURL, e);
}
是否会发生文件未完全读取的情况?(因为网络问题或其他原因?)。如果是,有没有办法测试它,甚至避免


背景:我正在开发一个应用程序(到目前为止还不是由我编写的),用户向我报告有时会丢失部分文件。它偶尔会发生,所以我唯一的猜测是,当文件被读入时,有时会出现故障,但我的java背景太少,无法确定…

是的,您会知道这是在根据

因此,您需要捕获异常,如下所示:

try {
    while (reader.ready()) {
            String line = reader.readLine();
    }
}
catch(IOException e) {
 // Bah! Humbug!
 // Should really log this too. So if you're using Log4j:
    log.error("Error reading from URL " + myURL.toString(), e);
} finally {

    try { if (reader != null) reader.close(); }catch(Exception e){}
}

是的,当您根据得到一个IOException时,您就会知道它发生了

因此,您需要捕获异常,如下所示:

try {
    while (reader.ready()) {
            String line = reader.readLine();
    }
}
catch(IOException e) {
 // Bah! Humbug!
 // Should really log this too. So if you're using Log4j:
    log.error("Error reading from URL " + myURL.toString(), e);
} finally {

    try { if (reader != null) reader.close(); }catch(Exception e){}
}

在这里的某个地方,我发现了以下评论:

就绪()!=有更多

ready()并不表示有更多数据要读取。它仅显示读取意志是否会阻止线程。在读取所有数据之前,它很可能会返回false

要确定是否没有更多数据,请检查readLine()是否返回null


听起来reader.ready()的实现导致了我的问题。这个假设我错了吗?

在这里的某个地方,我发现了以下评论:

就绪()!=有更多

ready()并不表示有更多数据要读取。它仅显示读取意志是否会阻止线程。在读取所有数据之前,它很可能会返回false

要确定是否没有更多数据,请检查readLine()是否返回null


听起来reader.ready()的实现导致了我的问题。这个假设我错了吗?

hmm,IOException被捕获,显然没有IOException,否则它会导致RuntimeException。唯一缺少的是finally部分。但那是在文件被读取之后,对吗?@July-这取决于错误处理代码在做什么。请更新示例以包含try{}catch{}finally{}块和错误处理代码。您不应该在while循环中使用ready()。ready()表示流在读取时不会阻塞,而不是表示没有剩余内容可读取。您需要使用read(),它将告诉您读取了多少字节,当没有剩余的内容可读取时返回-1(-1表示您已经完成了)。嗯,IOException被捕获,显然没有IOException,否则它将导致RuntimeException。唯一缺少的是最后一部分。但那是在文件被读取之后,对吗?@July-这取决于错误处理代码在做什么。请更新示例以包含try{}catch{}finally{}块和错误处理代码。您不应该在while循环中使用ready()。ready()表示流在读取时不会阻塞,而不是表示没有剩余内容可读取。您需要使用read(),它将告诉您读取了多少字节,当没有剩余的内容可读取时,它将返回-1(-1表示您已经完成了)。您不应该在while循环中使用ready()。ready()表示线程不会阻塞,而不是表示没有任何东西可以准备。您需要使用read(),它将告诉您有多少字节已准备就绪,当没有剩余内容可读取时,它将返回-1。您不应该在while循环中使用ready()。ready()表示线程不会阻塞,而不是表示没有任何东西可以准备。您需要使用read(),它将告诉您有多少字节已准备就绪,当没有剩余的内容可读取时,它将返回-1。