I’ve been having issues with a Java File. It’s designed to write line after line in a test file as a log. Unfortunately it overwrites the same line every time I call it.
If anyone can help I would be eternally grateful as this has been driving me up the wall!
Code Below.
public abstract class Log {
protected static String DefaultLogFileLocation = "c:\\LOG.txt";
public static void ToFile(String pInputString) {
FileOutputStream pOUTPUT;
PrintStream pPRINT;
try
{
pOUTPUT = new FileOutputStream(DefaultLogFileLocation);
pPRINT = new PrintStream(pOUTPUT);
pPRINT.println (pInputString + "\n");
pPRINT.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
}
}
You forgot to pass constructor parameter to specify you need to append data to file.
Also, why you don’t use some Java Logging Framework? E.g. java.util.logging or log4j
Example of log4j configuration to write to file: