0

Hi there I'm trying to write strings to a textfile but there is a little problem. I completed my code with the help of the other questions at this site but when i try to add strings to a text file it erases everything in that text file and writes the input. But I want it to go to the nextline and write it. I couldn't solve it. I would appreciate any help. Thank you..

public static void addCar() throws IOException{

        String string = transferBrand;
        String string2 = ":"+transferModel;
        System.out.println(string+string2);
        File file = new File("HatchBack.txt");
        try {
            StringReader stringReader = new StringReader(string+string2);
            BufferedReader bufferedReader = new BufferedReader(stringReader);
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for(String line = bufferedReader.readLine(); line != null; line =bufferedReader.readLine()) {               
                bufferedWriter.write(line);
                bufferedWriter.newLine();                   
            }
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }       
}
dgknrdm
  • 15
  • 5

1 Answers1

1

(untested) Did you try this as mentioned in JavaDoc?

FileWriter fileWriter = new FileWriter(file, true);
ITL
  • 422
  • 1
  • 5
  • 17
  • Well that was pretty easy I can't believe how i didn't see that Thank you it works now :/ – dgknrdm Apr 19 '14 at 15:54
  • Sometimes man does not to see the wood for the trees :) Just mark the answer as solution to close the question. – ITL Apr 19 '14 at 15:57