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"
Related
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
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));
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
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a Java String from the contents of a file
How to set a java string variable equal to “htp://website htp://website ”
I am making aprogram because I have about 50 pages of a word document that is filled with both emails and other text. I want to filter out just the emails. I wrote the program to do this which was very simple, not I just need to figure away to store the pages in a string variable.
I have a text file imported into my code File f=new File("test.txt"); Now I just need to find a way to save the text in this file into a String variable. Any help?
Okay, so based on your edit you're working with a text file and not a word file (two very different things ;) )
This is pretty simple, you just use a scanner class as such:
File file = new File("data.txt");
String s = new String();
try{
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
s += sc.nextLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Now bear in mind, if you just want a single line at a time from your text file (I have no idea what it is you're looking for, or what the data looks like) it's more likely that you'd want to store your data as an array of Strings or a linked list