I have a filewriter that takes String data from the user and writes it on the file. But the filewriter replaces the already existing data in that file. How do I prevent it from doing it? I just want to keep adding information without writing over something.
Here is the code
String info = scan.nextLine();
File myFile = new File ("/home/greg/workspace/Mavericks/fred.txt");
FileWriter writer = new FileWriter (myFile);
writer.write(register);
writer.flush();
Thanks. I fixed that. Now I just want to make the writer write using spaces. When I write to the file it just keeps writing within the same line.
You need use the boolean value true. From docs
public FileWriter(String fileName,
boolean append)
throws IOException
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.
Use second parameter of FileWriter, which is defining if you want to append or not. Just set it to true.
BufferedWriter writer = new BufferedWriter(new FileWriter(
sFileName, true));
Add \n after each line.
Use
new FileWriter(myFile, true)
The second parameter is for appending.
Api:
FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
FileWriter has an optional boolean in the constructor, which declares if it should be appended or not.
FileWriter writer = new FileWriter(..., true)
BufferedWriter bw = new BufferedWriter(new FileWriter(filename ,true));
Related
I have this method that writes to a file every time it's called:
public void writeToFile(String ins) {
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(ins);
bw.newLine();
bw.close();
fw.close();
}
But it only writes on the very first line of the file.
So, if I called it once with "Hello" and then again with "World", the file would contain "World", but the result I'm looking for is:
Hello
World
I tried using BufferedWriter.newLine() before and after writing the string but the result is the same?
You have to use FileWriter(String fileName, boolean append)
FileWriter fw = new FileWriter(f, true);
read the documentation of FileWriter:
FileWriter(File file, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
and you see, that you need to set the append value to true:
FileWriter fw = new FileWriter(f, true);
The point is: your code does what it is supposed to do - it uses a FileWriter, which by default will create a new, empty file; it writes one string; and closes the FileWriter.
If you want to write more than one line; you either have to
use the FileWriter in APPEND mode when doing later writes (by using that second, boolean argument for the FileWriter constructor with true)
change your method to take a list of strings, and write all of them at once
you can use a escape character:
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
I recommend you to use resource try to allow java to close the file when it will necessary
public void writeToFile(String ins) {
String fileName= "file.txt";
try (FileWriter fileWritter = new FileWriter(fileName, true)) {
fileWritter.write(ins + "\r\n");
} catch (IOException ex) {
}
}
give to this method a empty string "" to insert in the file a "Enter"
Here is what I am working with basically (example file):
Line 1: 213124
Line 2: 243223
Line 3: 325425
Line 4: 493258
Line 5: 359823
Is there a way to make PrintWriter begin to write to a file with 5 lines shown above, but so that it only writes AFTER line 5? So like I want to use
PrintWriter log = new PrintWriter("blah.txt");
log.println("52525")
and I want it to write that to line 6, not overwrite line 1.
EDIT: For anyone with a similar problem, you want to figure out how to append your files. As of my writing this, two people showed how below
To append to an existing file use "append" mode:
FileOutputStream fos = new FileOutputStream(filename,true);
PrintWriter pw = new PrintWriter(fos);
The true argument to the FileOutputStream constructor sets append mode.
To append to a file, you need to use the FileWriter(String fileName, boolean append) constructor:
try (PrintWriter log = new PrintWriter(new FileWriter("blah.txt", true))) {
log.println("52525");
}
If you're going to write a lot of output, then a BufferedWriter may be good, and if you need to specify the character encoding, you need to wrap a FileOutputStream with an OutputStreamWriter. This makes the chain much longer:
try (PrintWriter log = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("blah.txt", true),
Charset.forName("UTF-8"))))) {
log.println("52525");
}
The PrintWriter(String fileName) you called is actually shorthand for:
new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName)))
I am working on a program that needs a lot of app data. I am trying to create a function that creates a file with the path/file name of the string path. Here's my code:
public static void CreateFile(String path) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter(path, "UTF-8");
writer.close();
}
What did I do wrong? Shouldn't it create a file?
you can refer to this code :
FileWriter fw = new FileWriter("C:\\FileW3.txt");// you can give path here
//or
FileOutputStream fos = new FileOutputStream("path name");
PrintWriter pw = new PrintWriter (new OutputStreamWriter(fos));
pw.write("Combo stream and writer + using PrintWriter's write() methood/n");
pw.println();
pw.println("now using PrintWriter's println() methood");
pw.flush();
pw.close();
Also
File f = new File("path and filename");
This wont create a file , the file object can be used as parameter in FileWriter or FileOutputStream to create and then write to that file.
File object is just abstract representation of file.
It seems that you want to create an empty file. For this, you can use Files.createFile or File.createNewFile (but it will require you to instantiate a File).
To create a non-empty file, just write something in it and it will be automatically created if it does not exist.
Please see this link in the doucmentation - Create a file object then call 'createNewFile()' method on the newly created object.
So i'm trying to write stuff to a file and it works but when i call this method more than once it removes the previous stuff i wrote with the new. So i wonder what should i do so the method wont remove my previous text that i've added to the file and adds the new text to the
next line.
public static void writetofile(String id, String content) throws IOException
{
try {
FileWriter filewriter = new FileWriter("Random.txt");
BufferedWriter out = new BufferedWriter(filewriter);
out.write(id+" "+ content);
out.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
FileWriter filewriter = new FileWriter("Random.txt", true);
As per java doc
Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
Parameters:
file - a File object to write to
append - if true, then bytes will be written to the end of the file rather than the beginning
There is constructor signature of FileWriter with a boolean parameter which controls append behavior. Providing that appending is what you want, you should construct the FileWriter with this boolean set to true:
FileWriter filewriter = new FileWriter("Random.txt", true);
Note, that in case of single parameter constructor the output is written to the beginning of the file (see the implementation of FileOutputStream which is wrapped by FileWriter), so calling single parameter constructor is equivalent to setting the boolean parameter to false.
I want to write in a file but in a way that it should not delete existing data in that file rather it should append that file. Can anybody please help by giving any example related to appending a file? Thank you
You should use the FileWriter(File file, boolean append) constructor with the boolean value true.
Example
File file = new File("c:/tmp/foo.txt");
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
pw.println("Hello, World");
pw.close();