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

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));

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

How to check if file exists knowing only file name? [duplicate]

This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 3 years ago.
In Java using Maven project we may read file's content as a stream by knowing only file's name, for example:
InputStream in = getClass().getResourceAsStream("/" + fileName);
But is there way to check if the file exists without indicating the whole path, just passing file name?
file.exists() can be used to check whether such a file exists or not.Like following:
File file = new File("filepath");
if(file.exists()){
// Do your stuff
}

i want to display these two values in a text file [duplicate]

This question already has answers here:
How do I save a String to a text file using Java?
(24 answers)
Closed 6 years ago.
I have two string variables containing some user input.
String name="neil";
String mobile="5555";
now I want to display neil an 5555 in a text file. Please help me to display this in a text file, I know how to display content of a file which is already exist, but donot know how to work with this case, please help me...
PrintWriter out = response.getWriter();
This should be changed to use "java.io.File" class.
Has been explained in this thread: How to use PrintWriter and File classes in Java?
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter out = new PrintWriter(file);
Something like this.
ps: check out "try-with-resources"

Write String to text-file in java [duplicate]

This question already has answers here:
How do I save a String to a text file using Java?
(24 answers)
Closed 7 years ago.
Wanna save some information that I parse from a JSON to a plain text into a file and I also want this information to not be overwritten every time you run the program. It's suppose to work as a simple error logging system.
So far have I tried this:
FileWriter fileWriter = null;
File file = new File("/home/anderssinho/bitbucket/dblp-article-analyzer/logg.txt");
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
...
String content = "------------------------------------";
fileWriter = new FileWriter(file);
fileWriter.write(content);
//fileWriter.write(obj.getString("title"));
//fileWriter.write(obj.getString("creators"));
//fileWriter.write(article.GetElectronicEdition());
But when I do this it seems that I overwrite the information all the time and I'm also having problem to save the information I wanna grab from the JSON-array that I've got.
How can I do to make this work?
FileWriter fooWriter = new FileWriter(myFoo, false);
// true to append
// false to overwrite;
where myFoo is the File name
See this link
use append:
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("logg.txt", true)));
see this:
How to append text to an existing file in Java
Can you be more elaborate on this? If the problem is just not able to append then you can just add an argument to the FileWriter saying it to append and not write from the beginning.
Check the constructor here:
public FileWriter(String fileName, boolean append) throws IOException
Official Java Documentation:
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
Parameters:
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning.
Throws:
IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason

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

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.

Categories

Resources