Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

How do I...

I have written the following method...

public void writeFile(String loggingData) throws java.io.IOException {
   BufferedWriter out;      
   File outfile;

   outfile = new File("e:","abc.test");  //  this is where I want the log to go...
   out = new BufferedWriter(new FileWriter(outfile));
   out.write(loggingData,0,44);
   out.flush();
   out.close();      
}


My question is how do I determine if the file already exists ('cause I want to append to the file, as in logging)... I don't see how I can use the File.exists()  method on an object that doesn't exist yet... I was thinking of using an 'if' statement, but then when I put the file declarations in the block, the compiler tells me that 'out' is not defined.
0
rmmarsh
Asked:
rmmarsh
1 Solution
 
ranakCommented:
use FileOutputStream which has the option to append to file

FileOutputStream fs=null;
                    try
                    {
                    fs = new FileOutputStream(fileName,true);
                    }
                    catch(IOException ioEx)
                    {
                    fs =  new FileOutputStream(fileName);
                    }
                    DataOutputStream dataLogFileName = new DataOutputStream(fs);
0
 
rmmarshAuthor Commented:
Does this method 'open' and 'close' (resulting in much overhead) each time?  I want to be able to create the object, have it persist until I explicitly close it, all the while being able to write to the file.
0
 
karthicrajaCommented:
a) in Log evertime you have to open and close because it is used to trace the Crash or abnormal termination. writing then and there will avoid buffering of the content.

b)Log may have to be a Singleton class so, instantiate as a static variable in the main class and use it, so that garbage collection will not occur unnecessarily.

This function will help you to append.

public void log(String _error) throws IOException
{
private BufferedWriter write;
boolean append = true;
write = new BufferedWriter(new FileWriter(fileName, append));
              write.write(_error,0,_error.length());  
              write.newLine();                    write.close();
}

hope this helps.
karthic
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rmmarshAuthor Commented:
this is basically what I had... also, I don't know what a  "Singleton" class is, so I really don't understand what you are talking about with regard to it... what I want to do is have a logging function which writes data to a file.  I do NOT want to create the object/open the file each time I want to do a write... I want to open the file (create the object) ONCE, write many different times, and finally CLOSE the file.  I am concerned about the overhead... I tried putting the code which creates the objects in an "if" block, but then I couldn't reference the file that I wanted to write to (why it was unreachable, I don't know)... is there a way to do this (like one used to do in C/C++)?
0
 
rmmarshAuthor Commented:
this is basically what I had... also, I don't know what a  "Singleton" class is, so I really don't understand what you are talking about with regard to it... what I want to do is have a logging function which writes data to a file.  I do NOT want to create the object/open the file each time I want to do a write... I want to open the file (create the object) ONCE, write many different times, and finally CLOSE the file.  I am concerned about the overhead... I tried putting the code which creates the objects in an "if" block, but then I couldn't reference the file that I wanted to write to (why it was unreachable, I don't know)... is there a way to do this (like one used to do in C/C++)?
0
 
rmmarshAuthor Commented:
see comments above...
0
 
falterCommented:
First after constructing outfile = new File()
you can test existance with
outfile.exists() returning a boolean.
So you can construct your FileWriter with:
 out = new BufferedWriter(new FileWriter(outfile.getAbsolutePath(),outfile.exists()));
This will give you a FileWriter which appends if the file already exists.

Second:
Single instance is means not to have a public constructor for your log class.
something like that

public class MyLog {
  static Mylog instance = null;
  private Mylog(){
      ...
        //create LogFile and so on
      instance = this;
  }
public static MyLog getInstance()
{
  if (instance == null)
  {
    return new MyLog();
  }
  else
  {
    return instance;
  }
}
public void synchronized write(String s) throws (...)
{
  // do something
}

use synchronized in write if you will access your Log in a multithreaded application.
 
}

So you can call the write method every where with
Mylog.getInstance().write(...);
or
MyLog log = MyLog.getInstance();
log.write(...);

The private comnstructor and the static getInstance method garants that only one
object of this class will be instantiated in one JVM.

At least it means write a separate class for your logging handling all the stuff and implementing singleinstance design pattern..
0
 
rmmarshAuthor Commented:
Thank you... I still don't understand how you can test for something that "exists()" after you create the object.  Doesn't the creation of the object imply that the file exists?
0
 
falterCommented:
rmmarsh ,

No!!!
exists() will not test the existance of the created File object, it will test if by your File Object described object exists in your case the physical File.

You didn't create anything with
File myFile = new File("myFile.txt");
on your harddisk just a Java Object describing a file in your filesystem and knowing how to get some Information about this file.

If you open a FileWriter than something will happen on your harddisk.

Or look at the Javadoc of java.io.File
Instance of this class represents a filename of a file or directory on the host file system .... (extract from Javadoc JDK1.1.7)
0
 
rmmarshAuthor Commented:
thanks, absolutely clear now...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now