Write the console output over several runs to a text file [duplicate] - java

This question already has answers here:
Print java output to a file
(5 answers)
Closed 9 years ago.
My question is related to this one. However, I am looking for a way to append the text file over several runs. is there a way to write console output to a text file without erasing the old runs information? I am working on 30+ classes and it would be tedious to change System.out.println statements so I prefer sticking with the System.setOut solution.
I have the following code based on #Mac answer
PrintStream out = new PrintStream(new FileOutputStream("aa.txt"),true);
System.setOut(out);
but the file aa.txtdoes not append the results, am I missing something here?

When you create a FileWriter or FileOutputStream for your file, pass true as a second argument to the constructor of the FileWriter or FileOutputStream.
FileWriter(File file, boolean append)

FileWriter and FileOutputStream provide a constructor with an append flag. Just modify the referenced code accordingly

You should use:
PrintStream out = new PrintStream(new FileOutputStream("output.txt"),true);
System.setOut(out);

Related

Writing to another line in java [duplicate]

If you work with FileOutputStream methods, each time you write your file through this methods you've been lost your old data. Is it possible to write file without losing your old data via FileOutputStream?
Use the constructor that takes a File and a boolean
FileOutputStream(File file, boolean append)
and set the boolean to true. That way, the data you write will be appended to the end of the file, rather than overwriting what was already there.
Use the constructor for appending material to the file:
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
So to append to a file say "abc.txt" use
FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true);

Write to an offset using DataOutputStream [duplicate]

This question already has answers here:
Inserting text into an existing file via Java
(8 answers)
Closed 6 years ago.
In my project, we are writing a file using DataOutputStream. We are writing different data types like short, byte, int and long and we are using respective methods in DataOutputStream like writeShort(), writeByte() etc.
Now, I want to edit one record in this file at a particular offset. I know the offset from which that record starts but I am not sure what is the right approach of writing to the file because only method in DataOutputStream supporting offset is the one which takes byte[].
I want to write the whole record which is a combination of different data types as mentioned above.
Can someone please tell me what is the correct approach for this?
In your case, you should use RandomAccessFile in order to read and/or write some content in a file at a given location thanks to its method seek(long pos).
For example:
try (RandomAccessFile raf = new RandomAccessFile(filePath, "rw")) {
raf.seek(offset);
// do something here
}
NB: The methods writeShort(), writeByte() etc. and their read counterparts are directly available from the class RandomAccessFile so using it alone is enough.

How to Prevent PrintWriter from overwriting a file in Java

How can i prevent prinwriter in java from overwriting what's inside of that particular file?
Ex. I have a student.txt file. I already have few names there. After running and modifying this How do I create a file and write to it in Java? whats inside of that file will be overwritten. I just want to add it to the new line.
Also, how can i possibly perform search?
PrintWriter out = new PrintWriter(new FileWriter("student.txt", true));
The true is the append parameter - which indicates whether the FileWriter should append to the file. If it was false it would overwrite existing data in the file.
What do you mean by "how can i possibly perform search"?

Redirect lots of system.out.println's to a .txt file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Print java output to a file
In a Java program, I have a long method, (which I don't think is important to post since it's not vital to the question) that has a large number of println statements to print status updates to the console.
Instead of having these printout to the console, I'd like them to go into a txt file where I can store them and review them later.
Is there a simple way to redirect the output without manually going through each println statement?
If not, what's the best way to go about this?
I have had to do this before, it isn't very neat and I hope you aren't doing it for a final piece of code, but I did:
PrintStream ps = new PrintStream("\file.txt");
PrintStream orig = System.out;
System.setOut(ps);
//TODO: stuff with System.out.println("some output");
System.setOut(orig);
ps.close();
The System class has a static method, System.setOut(PrintStream out). All you have to do is create your own PrintStream that writes to a file, stuff it into this method and you're all set.
Better less fragile solution: Don't use System.out.printFoo(...) but instead just use a logger, and change the logging levels as it suits your purposes at that time.
add extra parameter to the method , PrintStream out
search&replace System.out with out in your method.
done.
(call method with a PrintStream wrapped around a FileOutputStream)

Writing data in an already made file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to append text to an existing file in Java
I ave a file already made in C:\myfile.txt and it has some data in it.. Now when I try to use File files = new File("C:\myfile.txt"); , It overwrites th orignal data and inserts the new data in it.. Is there a mechanism to aviod overwriting of old data?...
You could use append mode in one of the file writer classes:
FileWriter writer = new FileWriter("myfile.txt", true);
use the method new FileOutputStream(File,true) to append to an existing file.
Provide true in the constructor to append the file as told above
For more detailed control, use RandomAccessFile

Categories