0

I want to read a xml file that is presnt on a remote location.I use the following code

URL url = new URL(myurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
String strOutput = "";
System.out.println("start now");
//my code hangs after  priting start now            
while ((inputLine = in.readLine()) != null) {
       strOutput = strOutput + inputLine;
}

//contol doesnot reach here sometimes
in.close();

Is this a good way to read a remote file.What is the network is slow?Please suggest

akshay
  • 5,235
  • 14
  • 39
  • 49
  • `"server"` is not a valid URL, it has to be `"http://server/file.xml"` or something like this. – phlogratos Jul 15 '11 at 10:33
  • If network is slow it will take very long to reach `in.close()` that's all (unless there is something else that causes time-out) – Nishant Jul 15 '11 at 10:35
  • I dont want to wait indefinitely, cant i time out? – akshay Jul 15 '11 at 10:35
  • I would recommend not doing `strOutput = strOutput + inputLine;`. This may get quite slow if you're reading large XML files. Instead use a `StringBuilder` and call the `append` method. Adding Strings together like that involves creating a copy of the original String, so as the String gets larger, this will get slower and slower. – agxs Jul 15 '11 at 10:36
  • IS my approach of reading file correct? – akshay Jul 15 '11 at 10:36
  • @ashkay - try calling in.ready() and checking thats it's return value is true, right before your while loop. Because your code itself looks fine (though as @agxs mentioned, you don't want to build your input string the way you have it now). – Perception Jul 15 '11 at 10:44
  • i observed that sometimes by conde gets hanged after calling in.ready.Any idea? – akshay Jul 17 '11 at 05:57

2 Answers2

2

The comments have already indicated that there are some poor practices. I'll attempt to list what could be the cause of the poor performance:

  • Using readLine(). You are assuming that the XML file contains several lines, each terminated by either \r\n, \r or \n. This need not be the case, resulting the scenario where readLine() will take quite some time to complete.
  • Concatenating String objects. Strings are immutable in Java. Concatenating Strings will create a new String object, and this can become expensive if the size of the resulting string is quite large. If your document's ranges from a few bytes to kilobytes, this might not be an issue, but if it runs into megabytes, you are asking for trouble. Use StringBuilder or StringBuffer instead.
  • Assumptions on network performance. It might be better to fetch the file and parse it, instead of opening a connection, buffering it's contents and then parsing it. The network might simply not be capable of responding to your requests to read the file in a timely manner. By pre-fetching the file, you avoid hitting the network on every readLine() that will require reading beyond the buffered contents.
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • how can i fetch the entire file at once?This file is obtained by calling a url of a webserver. – akshay Jul 15 '11 at 12:46
  • The same way as you are doing it now. Except that you ought to be writing to a FileOutputStream instead of concatenating to a String. And, better use `read()` instead of `readLine()`. See some of the answers in this [question](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java). – Vineet Reynolds Jul 15 '11 at 12:51
0

Why don't you consider using JAXB or other sophesticated XML readers they are optimized in reading XML files.

Suken Shah
  • 1,622
  • 14
  • 20