I want to know how to write a String to an already existing file that is on my desktop.
This text file will already have information in it so I want it to be wiped.
Thank You
I want to know how to write a String to an already existing file that is on my desktop.
This text file will already have information in it so I want it to be wiped.
Thank You
The following code does write some text to the file without deleting the previous data.
BufferedWriter bw = null;
try
{
bw = new BufferedWriter(new FileWriter("sample.txt",true));
}catch(IOException e)
{
System.out.println("ERROR:"+e);
return;
}
try
{
bw.write("\r\ntext you want to write to the file");
bw.close();
}catch(IOException e)
{
System.out.println("ERROR:"+e);
}
}
Now if you want the data to be deleted then just replace the line:
bw = new BufferedWriter(new FileWriter("sample.txt",true));
with the following:
bw = new BufferedWriter(new FileWriter("sample.txt"));
Try this code
PrintWriter p = new PrintWriter(new FileWriter("yourLocation.txt"));
String s = "hello world";
p.printf(s);
p.close();