0

Can someone tell me why my data is not printing to my file. It will create the text file but it will not write to it.

Here is a snippett:

 out = new PrintWriter("completeAuto.txt");

            while ((car = br.readLine()) != null && (van = br1.readLine())!=null && (truck = br2.readLine()) != null) 
            {
               out.print (car );                    
               out.print(truck); 
            } 

Thanks

Edited by mike_2000_17: Fixed formatting

4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by peter_budo
0

The PrintWriter(String s) constructor does not contain automatic flushing to the writer. You need to use println statements to do this rather than print statements. Or immediately after your while loop call out.flush().
Also, I think maybe you need or statements in your while loop as opposed to and statements, but that may depend on what you are actually trying to achieve.

0

Small example how to write into file

import java.io.*;
class FileWrite 
{
   public static void main(String args[])
  {
      try{
    // Create file 
    FileWriter fstream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(fstream);
    out.write("Hello Java");
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.