Home arrow static arrow Java Programming [Archive] - Filewriting with 'FileWriter' Class
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Filewriting with 'FileWriter' Class
This topic has 1 reply on 1 page.

Posts:1
Registered: 7/21/04
Filewriting with 'FileWriter' Class  
Jul 21, 2004 5:36 AM



 
Hi,
the following works, but each time I use it, it overwrites the created file. So at the end I only have the last entry in the log file

public class SystemMessage
{
Date datum;
FileWriter fw;
String VisualString = new String("");

public SystemMessage()
{
fw = new FileWriter("D:
DataManager.log");
}

public void print(String Message, String className)
{
datum = new Date();
VisualString = datum.toString() +
" ["className"]" +
System.getProperty("line.separator") +
" " + Message +
System.getProperty("line.separator");

try
{
fw.write(VisualString,0,VisualString.length());
}
catch (IOException e)
{
System.err.println(e);
}
}
}

if I use this viarant, of which i thought it would work, nothing will be written to the file or I don't know what will be written. If I open the file it is empty!

public class SystemMessage
{
Date datum;
FileWriter fw;
String VisualString = new String("");

public SystemMessage()
{
}

public void print(String Message, String className)
{
datum = new Date();
VisualString = datum.toString() +
" ["className"]" +
System.getProperty("line.separator") +
" " + Message +
System.getProperty("line.separator");

try
{
fw = new FileWriter("D:
DataManager.log");
fw.write(VisualString,0,VisualString.length());
fw.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
}


maybe it's because of the .close() method. If it's where do I have to place it in my first variante?

I hope somebody is able to help me!
 

Posts:1,294
Registered: 02-08-30
Re: Filewriting with 'FileWriter' Class  
Jul 21, 2004 5:40 AM (reply 1 of 1)



 
new FileWriter("D:\\DataManager.log",true);


Second parameter takes a boolean that allows append mode
 
This topic has 1 reply on 1 page.