I've got a problem with a few classes at the moment that read from a text file containing a list of urls(XML files off the same server) I am attempting to stream the url that is read from the file and write it locally to a new file on disk.
testXML.java
GetTabXml.javapackage tabxmlbuilder; import java.io.*; import java.net.URL; public class testXML { public testXML(String s) throws Exception { String urlin = s; System.out.println(s); // getTabXml passes every second line read from the text file to s for some reason URL url = new URL(urlin); File file = new File("temp.txt"); FileWriter fw = new FileWriter(file); BufferedReader bs = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter bw = new BufferedWriter(fw); while (bs.read() != -1) { System.out.println(bs.readLine()); // When i remove this line the null pointer exception disappears but it appears to still print out the data I actually want written to disk bw.write(bs.readLine()); bw.flush(); } } }
package tabxmlbuilder; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class GetTabXml { public GetTabXml() throws IOException { FileReader fr = new FileReader("dates.txt"); BufferedReader br = new BufferedReader(fr); while (br.readLine() != null) { try { new testXML(br.readLine()); } catch (Exception ex) { Logger.getLogger(GetTabXml.class.getName()).log(Level.SEVERE, null, ex); } } } }
Errors
Below disappears when I remove the printline in filewriter loop.
Jun 10, 2012 4:56:48 PM tabxmlbuilder.GetTabXml <init> SEVERE: null java.lang.NullPointerException at java.io.Writer.write(Writer.java:157) at tabxmlbuilder.testXML.<init>(testXML.java:18) at tabxmlbuilder.GetTabXml.<init>(GetTabXml.java:17) at tabxmlbuilder.BuildTabXmlDB.main(BuildTabXmlDB.java:130)
This eventually happens
Jun 10, 2012 5:16:11 PM tabxmlbuilder.GetTabXml <init> SEVERE: null java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at java.io.BufferedInputStream.read1(BufferedInputStream.java:273) at java.io.BufferedInputStream.read(BufferedInputStream.java:334) at sun.net.www.MeteredStream.read(MeteredStream.java:134) at java.io.FilterInputStream.read(FilterInputStream.java:133) at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2968) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.readLine(BufferedReader.java:317) at java.io.BufferedReader.readLine(BufferedReader.java:382) at tabxmlbuilder.testXML.<init>(testXML.java:18) at tabxmlbuilder.GetTabXml.<init>(GetTabXml.java:17) at tabxmlbuilder.BuildTabXmlDB.main(BuildTabXmlDB.java:130)
The main problem that I can pass one url to GetTabXml and it works fine(writes the buffer to temp.txt) until I put it in a loop. Not sure what I am missing. Do I have to wait till the inputstream finishes the first request? I am new to java also. Thanks for your help.