Java: How to save console output to a text file? [duplicate] - java

This question already has answers here:
How to write console output to a txt file
(11 answers)
Closed 8 years ago.
For example, in the code, I have: System.out.println("Hello World");
The console will print: Hello World
So, I want to save the console output into a text file. Can anyone please hint me through this?

Create a file, and set as the out of the System class.
File file = new File("out.txt"); //Your file
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println("This goes to out.txt");

System class provide you a way to dump output in different stream which is System#setOut(PrintStream out)
Using this method you can pass you FileInputstream to System.setOut and you can save the console output.
PrintStream printStream = new PrintStream(new FileOutputStream(file));
System.setOut(printStream);
One interesting part of this question is though out is declared as final in System class but still you reassign this by System#setOut.

Related

How to save all the System.out.println present in the code in a .txt file? [duplicate]

This question already has answers here:
System.out.println to text file
(3 answers)
Closed 2 years ago.
Running my code, I would like to save all the System.out.println() in a file.
For example:
System.out.println("Save this!");
I would need a file .txt in which is stored the string "Save this!".
Someone could help me?
Regards,
Francesco Campanile
Use this to Write console to a file.
PrintStream out = new PrintStream(
new FileOutputStream("output.txt", true), true);
System.setOut(out);
The output file will be on your project's root directory

PrintStream - how to write inside an already created file [duplicate]

This question already has answers here:
File Write - PrintStream append
(2 answers)
Closed 6 years ago.
I want to write the results of my tests to an already created text file using PrintStream. Unfortunately i only know about this statement
PrintStream out = new PrintStream(new FileOutputStream("Results.txt"))
but this is creating a new file. Removing new from the statement won't work.
So, what is the command to write in Results.txt without deleting the previous results/creating a new one?
Use this constructor to open the file in append mode :
public FileOutputStream(String name, boolean append)
i.e.
PrintStream out = new PrintStream(new FileOutputStream("Results.txt",true));

give read write permission to file [duplicate]

This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
Closed 6 years ago.
hello guys I want to give file permission to open in read mode or write mode
.ext contains file extention and file_name contains name of file. f_p is a veriable where I an geting input as 'r' or 'w' mode. Here I am using same file at different locations
But in this code I am getting error as
cannot find symbol: method setReadable(boolean)
location: fos2 is of type FileOutputStream
<%
some code here
FileInputStream fis2 = new FileInputStream("e:/profile/epy/"+file_name+".ext");
FileOutputStream fos2 = new FileOutputStream("e:/decrypt/"+file_name+"."+ext);
if(f_p.equals("R")||f_p.equals("r"))
{
fos2.setReadable(true);
}
else if(f_p.equals("W")||f_p.equals("w"))
{
fos2.setWritable(true);
}
// some code here
%>
https://jsfiddle.net/wc8pccyL/
The current code uses the wrong class (FileOutputStream).
File f = new File(SOME_PATH);
if ("r".equalsIgnoreCase(f_p)) {
f.setReadable(true);
...
}
if ("w".equalsIgnoreCase(f_p)) {
f.setWritable(true);
...
}
However, one should be careful in assuming that one would want write access without read access. The assumption in the OP's code is that the f_p has a single value of "R" or "W", and sets the permission. This assumption should be carefully checked, especially across operating systems.
Also, if the FileOutputStream has to be used later (for actual output), it has a constructor that takes a File object, so there is nothing lost by creating the File object in such a scenario, and then creating the FileOutputStream fos = new FileOutputStream(f); where 'f' is the previously instantiated File object.

How do I save the console output to a textfile in a certain directory? [duplicate]

This question already has answers here:
Copy STDOUT to file without stopping it showing onscreen
(7 answers)
Closed 7 years ago.
I'd like to keep the console displaying the output but also keep it in a textfile as a log. Is there any way to do this? I tried doing this:
PrintStream printStream = new PrintStream(new FileOutputStream(locations[0][0] + "-" + locations[0][1] + "-output.txt"));
System.setOut(printStream);
But it doesn't display the output in the console anymore and it saves it in the project directory. Is there a way to do both?
You need to use a stream which writes to two destination streams (file and stdio). For example, use Apache's TeeOutputStream:
// Directory to save log files in
File logDir = new File("/var/log/myapp");
// Name of log file
String logFileName = locations[0][0] + "-" + locations[0][1] + "-output.txt";
// Actual log file
File logFile = new File(logDir, logFileName);
// File stream
OutputStream fileStream = new FileOutputStream(logFile);
// Stdout
OutputStream stdioStream = System.out;
// A stream redirecting output to both supplied streams
OutputStream combinedStream = new TeeOutputStream(stdioStream, fileStream);
// Finally use the resulting stream
System.setOut(new PrintStream(combinedStream));
Another alternative (out of the JAVA scope) is to use the *nix tee tool:
java -cp ... com.package.MyClass | tee /var/log/myapp/mylogfile.txt

Best way in Java to write in the middle of a file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best Way to Write Bytes in the Middle of a File in Java
I'm writing a program that modifies a PostScript file to add print properties, so I need to add lines in the middle of the file. These PostScript files are very large, and I want to know if the code I'm using is the most efficient. This code reads the source file and writes a temporary file adding a line where is needed.
Writer out = null;
Scanner scanner = null;
String newLine = System.getProperty("line.separator");
scanner = new Scanner(new FileInputStream(fileToRead));
out = new OutputStreamWriter(new FileOutputStream(fileToWrite));
String line;
while(scanner.hasNextLine()){
line = scanner.nextLine();
out.write(line);
out.write(newLine);
if(line.equals("%%BeginSetup")){
out.write("<< /Duplex true /Tumble true >> setpagedevice");
out.write(newLine);
}
}
scanner.close();
out.close();
Any help will be appreciated, thanks in advance.
Most of the old answers found on SO uses/links the old java.io.*
Oracle has nice examples how to do this using the "new" java 7 java.nio.* packages (usually with much better performance)
http://docs.oracle.com/javase/tutorial/essential/io/rafs.html
See RandomAccessFile and its example here: Fileseek - You can seek to a particular location in the file and write to it there.

Categories

Resources