Fred
Fred

Reputation: 49

Java - One method to open a file another to write and another to close

All.. task - Open file but don't write anything. When data comes from a thread and request to log it, write it to the file. When request to stop logging, close the file. File should be initially opened in append mode. I have written such code in many languages but not in Java. Everything I come across talks about a single method to open, write(s) and close. What I need is to open a the file and return a pointer to the file (FileWriter). Use that file pointer later to both write to and eventually close the file. My code is below:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class LogIt
{
  public static FileWriter outLog = null;

  public static void main(String[] args) throws IOException 
  {
   Logging lFile = new Logging() ;
   Logging.OpenLogFile(primeClass.theLogFile);
   Logging.CloseLogFile() ; 
  } 
// end main

public static void OpenLogFile(String filename) throws IOException 
{
// open output file
try  
 {
    // if file doesn't exists, then create it

    File file =new File(filename);
    if (!file.exists())
      {
        file.createNewFile();
        System.out.println("Logfile " + filename + " did not exist!");
      }
    FileWriter outLog = new FileWriter(filename, true);
    System.out.println("logFile " + filename + " has been opened for append mode");        
 }
catch  (IOException e)
   {
      System.out.println("Unable to Open log file " + filename);
      System.err.println("Error: " + e.getMessage());
   }
}
// End of OpenLogFile

public static void WriteLogFile(String line2Write) throws IOException 
{
// write output to opened file
try  
 {
  outLog = new FileWriter(line2Write);
  outLog.write(line2Write);
 }
catch  (IOException e)
   {
     System.out.println("Unable to write to " + PrimeClass.theLogFile);
     System.out.println("Error: " + e.getMessage());
   }
} // end OpenLogFile

public static void CloseLogFile() throws IOException 
{
 try  
 {
  outLog.close();
  System.out.println("file closed");
 }
 catch  (IOException e)
   {
   System.out.println("Unable to close logfile " + PrimeClass.theLogFile);
   System.out.println("Error: " + e.getMessage());      
   }
} 
// end CloseLogFile

} 
// end LogIt class

The class compiles and if the file doesn't exist its created. However, without writing to the file, an attempt to close the file results in a null pointer exception. I was thinking of building 2 methods to set and get the "outLog" value. Also, it appears that when the write occurred it actually started using the output to generate files on the directory and not written to the actual file.

Suggestions?

Upvotes: 0

Views: 1164

Answers (1)

user207421
user207421

Reputation: 310884

You're declaring a local FileWriter instead of assigning to the member variable.

NB You don't need to create the file if it doesn't exist. `new FileWriter()' already does that.

Upvotes: 0

Related Questions