-2

I am new to java and I am writing coding for automation in Selenium. I want to know is it possible to write a text file throughout the java program till end of the program. I have tried to write a text file using bufferedWriter.write(). First three lines are writing and after that it is not at all writing any text. I have tried flush.

My question is,do we have possibility to open file at starting of the program and write lines when ever we need in the middle of the program and as many number of lines and close the file end of the program?

If possible, request to share any same code. I am fine with any file write method (File Writer, Buffered writer, file output stream)

Dina
  • 11
  • 2

1 Answers1

1
String filename="fileToRead.txt";
    FileWriter fw=null;
    try {
        fw = new FileWriter(filename,true);
        BufferedWriter bw=new BufferedWriter(fw);
        PrintWriter out = new PrintWriter(bw);

        String text="";
        out.write(text);
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//do stuff
//open streams again and write

true as parameter=>append mode:

fw = new FileWriter(filename,true);
dskfdskjgds
  • 341
  • 3
  • 14
  • Would be worth actually explaining the flow to the OP, as it appears they didn't understand any of the examples out on the web. –  Aug 04 '15 at 09:39
  • @dskfdskjgds Thanks for the Code. – Dina Dec 17 '15 at 20:13