0

The title says it all. This is my ResultSet code:

try {
    while (rs.next()) {
        int ID  = rs.getInt("ID");
        String Name = rs.getString("Name");
        String CountryCode = rs.getString("CountryCode");
        String District = rs.getString("District");
        int Population = rs.getInt("Population");

        //Display values
        try {
            writeToFile(ID, Name, CountryCode, District, Population);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

This is my writeToFile method:

public void writeToFile(int id, String name, String countryCode, String district, int population) throws IOException {
    FileWriter fw = new FileWriter("C:\\Users\\horry\\Desktop\\results.txt");
    fw.write(id + " " + name + " " + countryCode + " " + district + " " + population + "\n");
    try {
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Ive tried the usual flush and close method but something is missing. Should the filewriter be declared elsewgere?

Todd
  • 30,472
  • 11
  • 81
  • 89
  • No exceptions. One line gets written but with an incorrect ID. Do you know a better method? – Neil A Jones Jun 05 '17 at 19:20
  • Can you use the debugger (or logging) to see if the name, countryCode, etc. variables are being populated (or write a test case that passes in hard coded values for these parameters)? – John Jun 05 '17 at 19:42

2 Answers2

2

Try to open file with append option

 FileWriter fw = new FileWriter("C:\\Users\\horry\\Desktop\\results.txt", true);

To add new line in any platform (e.g. linux ) use general line seperator as below

fw.write(id + " " + name + " " + countryCode + " " + district + " " + population + System.getProperty("line.separator"));
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
-1

You are opening a new FileWriter for each iteration of your ResultSet. By default a FileWriter writes at the beginning of the file. So you are continously overwritting what you have written in the file at the previous iteration.

Furthermore, I would advice to not create a new FileWriter at each iteration. I would open a FileWriter just one time and use the same one for all the ietrations.

Then, use a BufferedWriter instead of a pure FileWriter. It will be much faster.

Also, close the writer in a finally block, not within the try. Since Java 7, you can even use try-with-resources.

Finally, use br.newLine(); to write a new line instead of managing the "\n" yourself.

Please check the code below

File f = new File("C:\\Users\\horry\\Desktop\\results.txt");
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
    while (rs.next()) {
        int id  = rs.getInt("ID");
        String name = rs.getString("Name");
        String countryCode = rs.getString("CountryCode");
        String district = rs.getString("District");
        int population = rs.getInt("Population");
        bw.write(id + " " + name + " " + countryCode + " " + district + " " + population);
        bw.newLine();
    }
}
Comencau
  • 1,084
  • 15
  • 35