English 中文(简体)
url 联系问题
原标题:url connection question
  • 时间:2011-07-07 12:57:41
  •  标签:
  • java
  • url

I m reading a URL with the following code:

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);
}

能否做到这一点,档案不是完全读吗? (由于网络问题或其他问题) 如果是,是否有办法加以测试,甚至避免?

背景: 我在申请中工作(我直到现在才提出),用户报告说,有些档案有时丢失。 偶然发生的情况是,我唯一的猜测是,在阅读档案时,有些事情有时失败了,但我很少敢肯定。

最佳回答

在其他地方,我发现以下意见:

ready() != has more

现有数据没有显示需要阅读的数据。 它只表明读物会阻碍阅读。 在你读到所有数据之前,这很可能会回错。

发现如果阅读数据证明无效,则不再进行数据核对。

令人信服的是,使用读写法(阅读版)的实施造成我的问题。 我错过这一假设吗?

问题回答

Yes, you ll know it s happened when you get an IOException as per the Reader.readLine docs.

因此,你希望<>副渔获物,例外,例如:

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){}
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签