rory-h
rory-h

Reputation: 680

JAVA - Writing sentences to textfile

I have this code below. Basically I'm getting an input from a given url. This website shows a sentence. Each time I reload the website it gets a new sentence and so on. So, I managed to get that working. Now I'm trying to write the sentence in a textfile. But something is wrong. It only writes the first line and nothing else. What's wrong with my code?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ReadIp {
public static void main(String[] args) throws MalformedURLException, IOException,     
InterruptedException {
    ReadIp readIP = new ReadIp();
    while (true) {
        readIP.getIP();
        Thread.sleep(2000);
    }
}

BufferedReader      buff;
InputStreamReader   inStream;
String              line;
URL                 url;
URLConnection       urlConn;
FileWriter fileWriter ;
BufferedWriter bufferedWriter;

public ReadIp() throws IOException {
    fileWriter = new FileWriter("myfile.txt", true);
    bufferedWriter = new BufferedWriter(fileWriter);
}

public void getIP() throws MalformedURLException, IOException {
    this.url = new URL("http://test.myrywebsite.co.uk");
    this.urlConn = this.url.openConnection();
    this.inStream = new InputStreamReader(this.urlConn.getInputStream());
    this.buff = new BufferedReader(this.inStream);

    try {
        while ((this.line = this.buff.readLine()) != null) 
        {
            System.out.println(this.line);
            try {
                this.bufferedWriter.write(this.line);
                this.bufferedWriter.write("\n");

                this.bufferedWriter.flush();

            } catch (IOException e) 
            {
            } 

        }
        if (this.bufferedWriter != null) 
        {
            this.bufferedWriter.close();
        }

       this.inStream.close();

    } 
    catch (Exception ex) 
    {
    }


}
}

Any help would be greatly appreciated. Thank you.

Upvotes: 0

Views: 942

Answers (2)

user85421
user85421

Reputation: 29680

The BufferedWriter is being opened in the constructor and is being closed in getIp. The constructor is called only once, but getIp is called every 2 seconds to read a sentence. So the BufferedWriter is being closed after the first line (and not opened again). The second call of getIp tries to write the second sentence but the BufferedWriter is closed. This should throw an Exception which is being ignored since the catch block is empty.

Never leave a catch block empty - as fgb wrote above!

First of all, at least add a printStackTrace() to each empty catch block, e.g.:

    catch (Exception ex) {
        ex.printStackTrace();
    }

so you can see if an Exception is being thrown...

I would suggest to open the BufferedWriter in the method getIp instead of the constructor; or, if it should stay open all the time, close the BufferedWriter in an additional method, called after the loop in main terminates

Upvotes: 0

Reimeus
Reimeus

Reputation: 159784

Move the statement

writer.close();

out of the inner try catch block so that you're not closing the OutputStream after writing the first entry to the file. The same applys to the InputStream

inStream.close();

Upvotes: 5

Related Questions