I am trying to write a method that makes a "log.txt file" if one does not already exist and then writes to the file. The problem that I am encountering is every time I call the method, it overwrites the existing log. How do I change the method so that instead of overwriting the data it just updates the file?
My Write File Method:
File log = new File("log.txt")
try{
if(log.exists()==false){
System.out.println("We had to make a new file.");
log.createNewFile();
}
PrintWriter out = new PrintWriter(log);
out.append("******* " + timeStamp.toString() +"******* " + "\n");
out.close();
}catch(IOException e){
System.out.println("COULD NOT LOG!!");
}
This question is related to
java
JFileChooser c= new JFileChooser();
c.showOpenDialog(c);
File write_file = c.getSelectedFile();
String Content = "put here the data to be wriiten";
try
{
FileWriter fw = new FileWriter(write_file);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(Content);
bw.append("hiiiii");
bw.close();
fw.close();
}
catch(Exception e)
{
System.out.println(e);
`}
try this one
public void writeFile(String arg1,String arg2) {_x000D_
try {_x000D_
if (!dir.exists()) {_x000D_
_x000D_
if (dir.mkdirs()) {_x000D_
_x000D_
Toast.makeText(getBaseContext(), "Directory created",_x000D_
Toast.LENGTH_SHORT).show();_x000D_
} else {_x000D_
Toast.makeText(getBaseContext(),_x000D_
"Error writng file " + filename, Toast.LENGTH_LONG)_x000D_
.show();_x000D_
}_x000D_
}_x000D_
_x000D_
else {_x000D_
_x000D_
File file = new File(dir, filename);_x000D_
if (!file.exists()) {_x000D_
file.createNewFile();_x000D_
}_x000D_
_x000D_
FileWriter fileWritter = new FileWriter(file, true);_x000D_
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);_x000D_
bufferWritter.write(arg1 + "\n");_x000D_
bufferWritter.close();_x000D_
_x000D_
} catch (Exception e) {_x000D_
e.printStackTrace();_x000D_
Toast.makeText(getBaseContext(),_x000D_
"Error writng file " + e.toString(), Toast.LENGTH_LONG)_x000D_
.show();_x000D_
}_x000D_
_x000D_
}
_x000D_
Here is a simple example of how it works, best practice to put a try\catch into it but for basic use this should do the trick. For this you have a string and file path and apply thus to the FileWriter and the BufferedWriter. This will write "Hello World"(Data variable) and then make a new line. each time this is run it will add the Data variable to the next line.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
String Data = "Hello World";
File file = new File("C:/Users/stuff.txt");
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(Data);
bw.newLine();
bw.close();
You can even use FileOutputStream
to get what you need. This is how it can be done,
File file = new File(Environment.getExternalStorageDirectory(), "abc.txt");
FileOutputStream fOut = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("whatever you need to write");
osw.flush();
osw.close();
BufferedWriter login = new BufferedWriter(new FileWriter("login.txt"));
is an example if you want to create a file in one line.
use a FileWriter instead.
FileWriter(File file, boolean append)
the second argument in the constructor tells the FileWriter to append any given input to the file rather than overwriting it.
here is some code for your example:
File log = new File("log.txt")
try{
if(!log.exists()){
System.out.println("We had to make a new file.");
log.createNewFile();
}
FileWriter fileWriter = new FileWriter(log, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("******* " + timeStamp.toString() +"******* " + "\n");
bufferedWriter.close();
System.out.println("Done");
} catch(IOException e) {
System.out.println("COULD NOT LOG!!");
}
For some reason, none of the other methods worked for me...So i tried this and worked. Hope it helps..
JFileChooser c= new JFileChooser();
c.showOpenDialog(c);
File write_file = c.getSelectedFile();
String Content = "Writing into file\n hi \n hello \n hola";
try
{
RandomAccessFile raf = new RandomAccessFile(write_file, "rw");
long length = raf.length();
System.out.println(length);
raf.setLength(length + 1); //+ (integer value) for spacing
raf.seek(raf.length());
raf.writeBytes(Content);
raf.close();
}
catch (Exception e) {
System.out.println(e);
}
You can change your PrintWriter and use method getAbsoluteFile(), this function returns the absolute File object of the given abstract pathname.
PrintWriter out = new PrintWriter(new FileWriter(log.getAbsoluteFile(), true));
Source: Stackoverflow.com