user3308219
user3308219

Reputation: 157

Java - Append text to end of file

I'm trying to write to a text file that will just contain big blocks of text that I was planning on dealing with later. There's a number of combinations of variables that I have to deal with and typing it all out is too tedious. I've tried running this code twice and all it does is overwrite the first line over and over again, giving me the last combination of variables possible. This is the code.

import java.util.*;
import java.io.*;
public class FileWritingThing {
    public static void main(String[]args) throws IOException{
        String precision = null;
        String criteria = null;
        String specLevel = null;
        String precondLevel = null;
        PrintWriter writer = null;      

        for(int i = 0; i < 3; i++){
            if(i == 0){
                precision = "Precision = 4;";
            }
            if(i == 1){
                precision = "Precision = 8;";
            }
            if(i == 2){
                precision = "Precision = 12;";
            }
            for(int j = 0; j < 3; j++){
                if(j == 0){
                    criteria = "Objective Function;";
                }
                if(j == 1){
                    criteria = "Domain;";
                }
                if(j == 2){
                    criteria = "Objective Function + Domain;";
                }
                for(int k = 0; k < 3; k++){
                    if(k == 0){
                        specLevel = "Speculation Level = 10000;";
                    }
                    if(k == 1){
                        specLevel = "Speculation Level = 100000;";
                    }
                    if(k == 2){
                        specLevel = "Speculation Level = 1000000;";
                    }
                    for(int l = 0; l < 3; l++){
                        if(l == 0){
                            precondLevel = "Precondition Level = 0;";
                        }
                        if(l == 1){
                            precondLevel = "Precondition Level = 25;";
                        }
                        if(l == 2){
                            precondLevel = "Precondition Level = 100;";
                        }
                        writer.println(precision + "\n" + criteria + "\n" + specLevel + "\n" + precondLevel + "\n");
                    }
                }
            }
        }
    writer.close();
}

}

/*try {
    writer = new PrintWriter(new BufferedWriter(new FileWriter("permutations.txt", true)));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
*/

All I need to know is how to append each block to the file line. In addition, I'm getting some weird problem where even though I've include "\n" to separate the variable listings, it all appears on one line regardless. The commented code at the bottom was originally included just before the "writer.println()" line.

Upvotes: 1

Views: 193

Answers (1)

anonymous
anonymous

Reputation: 1317

If the commented lines are just above the writer.println(), then it might explain the strange behaviour you're experiencing. You should create the PrintWriter outside the for-loop. Something like this

PrintWriter writer = null;
try {
   writer = new PrintWriter(new BufferedWriter(new FileWriter("permutations.txt", true)));
    // Most outer for loop goes here
    for(int i=0......) {
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch(Exception e) { e.printStackTrace(); }
finally {
    if(writer!=null) {
        try {
            writer.flush();
            writer.close();
        } catch(Exception ignore) {}
    }
}

Upvotes: 1

Related Questions