Jessica Fede
Jessica Fede

Reputation: 37

file input out append

I am trying to write some coordinates to a file and later read it in as a string. So I need to have them written to file attached...without space or a new line, but my code writes only the first coordinate, that is pos_Let, but does not write pos_Num at all, not even with a space or on a new line.

So how can I get the code to write to file pos_LetposNum like that? Obviously I mean their references ;) ..thanks in advance

protected  void writeCoordtoFile () throws IOException
  {
       File file = new File("FermiPresentCoord.txt");
       boolean yes = file.createNewFile() ;
       //boolean yes = exists();
       if (yes == true)
       {
       // create a FileWriter Object
       FileWriter writer = new FileWriter(file, true);

       // Writes the content to the file
       writer.write("");
       writer.flush();
       writer.write(pos_Let);
       writer.flush();
       writer.write(pos_Num);
       writer.close();
       }
       else
       {
           // creates the file
           file.createNewFile();

           // creates a FileWriter Object
           FileWriter out = new FileWriter(file);

           // Writes the content to the file
           out.write(pos_Let);
           out.flush();
           out.write(pos_Num);
           out.flush();
           out.close();
       }
  }  

Upvotes: 1

Views: 71

Answers (2)

STaefi
STaefi

Reputation: 4377

I can not find out why you are checking the existence of the output file. Because, when you are using FileWriter if the file specified in the path does not exist, it would create it and open a character output stream to it. Also if it exists in that path, only opens the output stream and it is ready to write into.

Try the following code and see whats happening when you run it more than one times:

float posLat = 156.23589965f; 
float posLon = 12.987564f; 
File file = new File("c:/FermiPresentCoord.txt");
FileWriter writer = new FileWriter(file, true);
writer.append(posLat+",");
writer.append(posLon+",");
writer.flush();
writer.close();

There is no need to invoke the file.createNewFile() and/or checking the for the existence of the file when you want to write into it.

The second argument for the FileWriter constructor is append flag. So every time you create an output stream to a file with FileWriter(file, true) constructor it automatically appends to the data of the file.

Good Luck.

Upvotes: 1

vefthym
vefthym

Reputation: 7462

Quoting the method createNewFile():

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

Returns: true if the named file does not exist and was successfully created; false if the named file already exists

in your case, you first create the file, and createNewFile() returns true, so you go to the if block, appending the line to the current file. Then, createNewFile() returns false, since, the file exists! So, you go to the else block, and create the file again from scratch...

So, basically, just inverse the if with else, and don't call createNewFile() twice... With the least possible changes (so that you do not get confused) here is my simple suggestion:

protected  void writeCoordtoFile () throws IOException
{
   File file = new File("FermiPresentCoord.txt");
   boolean fileDoesNotExist = file.createNewFile() ;
   //boolean fileDoesNotExist = file does **not** exist!
   if (fileDoesNotExist)
   {
       // create a FileWriter Object
       FileWriter writer = new FileWriter(file);

       // Writes the content to the file
       writer.write(pos_Let);
       writer.write(pos_Num);
       writer.close();
       
   }
   else
   {
       // creates a FileWriter Object
       FileWriter out = new FileWriter(file,true);

       // Writes the content to the file
       out.write(""); //not sure why you need that...
       out.write(pos_Let);
       out.write(pos_Num);
       out.close();
   }
}  

Upvotes: 1

Related Questions