Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Socket Problem?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Reading File/Socket Problem?

    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
    package 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();
            }
        }
    }
    GetTabXml.java
    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.
    Last edited by danza; June 10th, 2012 at 07:31 AM. Reason: Title


Similar Threads

  1. Re: Java socket server problem
    By viperhastle in forum Java Networking
    Replies: 1
    Last Post: May 27th, 2012, 12:21 PM
  2. Socket problem
    By Robtn in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 15th, 2011, 04:50 PM
  3. [SOLVED] Problem in socket programming
    By sinni in forum Java Networking
    Replies: 1
    Last Post: March 15th, 2011, 11:26 AM
  4. Problem reading from server socket
    By glauber in forum Java Networking
    Replies: 0
    Last Post: February 15th, 2011, 12:54 PM
  5. [SOLVED] Socket Programming Problem
    By relixus in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2010, 10:35 PM