I've tried using BufferWriter format as well as FileWriter and PrintWriter each with a boolean true statement but they both behave the same as if I simply used a simple new File. Each time I get to the end of my program run, I call the function that writes the saved data to be appended. What ends up happening is that it overwrites the last saved data. I also have other code blocks that handle that text file as well and reformating them did nothing either.
//saves user information to "Users.txt" which will be called in the finally block after choice switch
public void writeUsers()
{
try{
File userFile = new File("Users.txt");
PrintWriter output = new PrintWriter(userFile);
for(User i: userList) {
output.append("Name:");
output.println(i.getrealName());
output.append("UID:");
output.println(i.getidName());
output.append("Password:");
output.println(i.getpassword());
output.println(" ");
}
output.close();
System.out.print("Information has been saved to Users.txt\n");
}
catch(FileNotFoundException fnf) {
System.err.println("FileNotFoundException: File Users.txt does not exist " + fnf);
}
catch(IOException eyeoh) {
System.err.println("IOException: Error writing to Users.txt " + eyeoh);
}
}
The constructor PrintWriter(File) truncates the output file by default. The fact that PrintWriter's method append() is called doesn't mean that it changes the mode of the file being opened. The behavior for append is described as:
An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation
out.write(csq.toString())
Here, you could use the constructor for PrintWriter that takes a FileOutputStream for appending
PrintWriter output =
new PrintWriter(new FileOutputStream(userFile, true /* append = true */));
You have to create the PrintWriter in append mode. Otherwise, when it first opens the file, it will clear it out. You can open it in append mode using:
new PrintWriter(new FileWriter(userFile,true)) // the `true` argument opens it in append mode
PrintWriter by defualt truncates all existing data. To append you could, as other answers suggest, add a "true" argument to the constructor, which denotes "append = true,"
However, this is done more elegantly using java.nio.file.Files along with java.nio.file.StandardOpenOption, in which you can specify StandardOpenOption.APPEND as opposed to StandardOpenOption.TRUNCATE_EXISTING
You can also specify things such as StandardOpenOption.CREATE which creates the file if it does not exist.
Additionally, remember to either place your output.close() statement in a finally block, or use try-with-resources. Otherwise if the flow of the program is interrupted (i.e. an Exception is thrown), output would remain unclosed. I personally use try-with-resources as it is less of a hassle: just declare all your resources, and they are closed automatically for you, whether or not the flow of the program is disrupted.
Also, as a general tip, print or pass the actual Exception object in your catch block, as opposed to just a "custom string" so as to not lose the original content of the Exception thrown. You can then concatenated that with whatever string you also want to print.
try(BufferedWriter bufWriter =
Files.newBufferedWriter(Paths.get("Users.txt"),
Charset.forName("UTF8"),
StandardOpenOption.WRITE,
StandardOpenOption.APPEND, //Makes this BufferedWriter append to the file, not truncate
StandardOpenOption.CREATE);
PrintWriter output = new PrintWriter(bufWriter, true);)
{
output.println("Text to be appended.");
}catch(FileNotFoundException e){
System.err.println(e + "Custom string");
}catch(IOException e){
System.err.println(e + "Something eyeoh occurred...");
}
This uses a try-with-resources statement to declare and create a BufferedWriter using java.nio.file.Files, which accepts StandardOpenOption parameters, and an auto-flushing PrintWriter (denoted by "true" in the constructor) from the resultant BufferedWriter. PrintWriter's println() method, can then be called to write to the file.
The StandardOpenOption parameters used in this code: opens the file for writing, only appends to the file, and creates the file if it does not exist.
Paths.get("path here") can be replaced with new File("path here").toPath(), if you are working exclusively with File objects (i.e. if you are using JFileChooser.getSelectedFile())
And Charset.forName("charset name") can be modified to accommodate the desired Charset.
Related
I am writing a small program to help with planning future workouts. I am nearly finished however saving and loading is giving me some trouble. The program works with a list of "ride"(a custom class) objects that hold a number of qualities (like a Dat, and then some ints and doubles)
right now, I have two methods, a "saver" and a "loader":
public void saver() {
try{ // Catch errors in I/O if necessary.
// Open a file to write to, named SavedObj.sav.
FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
// Create an ObjectOutputStream to put objects into save file.
ObjectOutputStream save = new ObjectOutputStream(saveFile);
// Now we do the save.
for (int x = 0; x < rides.size(); x++) {
save.writeObject(rides.get(x).getDate());
save.writeObject(rides.get(x).getMinutes());
save.writeObject(0);
save.writeObject(rides.get(x).getIF());
save.writeObject(rides.get(x).getTss());
}
// Close the file.
save.close(); // This also closes saveFile.
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
public void loader() {
try{
// Open file to read from, named SavedObj.sav.
FileInputStream saveFile = new FileInputStream("SaveObj.sav");
// Create an ObjectInputStream to get objects from save file.
ObjectInputStream save = new ObjectInputStream(saveFile);
Ride worker;
while(save.available() > 0) {
worker = new Ride((Date)save.readObject(), (int)save.readObject(), (double)save.readObject(), (double)save.readObject(), (int)save.readObject());
addRide(worker.getDate(), worker.getMinutes(), 0, worker.getIF(), worker.getTss());
}
// Close the file.
save.close(); // This also closes saveFile.
}
catch(Exception exc){
exc.printStackTrace(); // If there was an error, print the info.
}
}
When I run the program, neither "save" nor "load" return any errors. A .sav file is created when one does not exist, and is edited each time the program is executed. Yet, the program never restores data from previous sessions. Please let me know if more information is required.
Thanks in advance for the help!
Don't use available() which returns the number of bytes that can be read without blocking. It doesn't mean what all bytes were read.
If your objects are never null, you could use Object readObject() to check if all data were read from the inputstream.
Date date = null;
while( (date = (Date)save.readObject()) != null) {
worker = new Ride(date, (int)save.readObject(), (double)save.readObject(), (double)save.readObject(), (int)save.readObject());
addRide(worker.getDate(), worker.getMinutes(), 0, worker.getIF(), worker.getTss());
}
Otherwise if read values may be null, you could serialize directly the Ride object or a class containing all fields to serialize rather than unitary fields which could be null With this, the check to know if all data were read with Object readObject() could work.
Do not use available() as a condition. It just tells you whether there is some byte available for immediate reading without any delay, it does not mean the stream has reached its end.
Also you should maybe add a BufferedInputStream and BufferedOutputStream between the Object and File streams, that's almost always a good idea.
To solve your issue you could e. g. first write an integer in the save method that tells you how many objects are in the file and on load read that integer and then make a simple for loop with this amount.
Or you could throw in a PushbackInputStream in the row and then as EOF check use its read() method. It will return -1 on EOF and you can abort reading. If it returns anything else, you unread() the read byte and use the ObjectInputStream that you placed on top.
How do i append an existing line in a text file? What if the line to be edited is in the middle of the file? Please kindly offer a suggestion, given the following code.
Have went through & tried the following:
How to add a new line of text to an existing file in Java?
How to append existing line within a java text file
My code:
filePath = new File("").getAbsolutePath();
BufferedReader reader = new BufferedReader(new FileReader(filePath + "/src/DBTextFiles/Customer.txt"));
try
{
String line = null;
while ((line = reader.readLine()) != null)
{
if (!(line.startsWith("*")))
{
//System.out.println(line);
//check if target customer exists, via 2 fields - customer name, contact number
if ((line.equals(customername)) && (reader.readLine().equals(String.valueOf(customermobilenumber))))
{
System.out.println ("\nWelcome (Existing User) " + line + "!");
//w target customer, alter total number of bookings # 5th line of 'Customer.txt', by reading lines sequentially
reader.readLine();
reader.readLine();
int total_no_of_bookings = Integer.valueOf(reader.readLine());
System.out.println (total_no_of_bookings);
reader.close();
valid = true;
//append total number of bookings (5th line) of target customer # 'Customer.txt'
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath + "/src/DBTextFiles/Customer.txt")));
writer.write(total_no_of_bookings + 1);
//writer.write("\n");
writer.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
//finally
// {
//writer.close();
//}
}
}
}
To be able to append content to an existing file you need to open it in append mode. For example using FileWriter(String fileName, boolean append) and passing true as second parameter.
If the line is in the middle then you need to read the entire file into memory and then write it back when all editing was done.
This might be workable for small files but if your files are too big, then I would suggest to write the actual content and the edited content into a temp file, when done delete the old one an rename the temp file to be the same name as the old one.
The reader.readLine() method increments a line each time it is called. I am not sure if this is intended in your program, but you may want to store the reader.readline() as a String so it is only called once.
To append a line in the middle of the text file I believe you will have to re-write the text file up to the point at which you wish to append the line, then proceed to write the rest of the file. This could possibly be achieved by storing the whole file in a String array, then writing up to a certain point.
Example of writing:
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path)));
writer.write(someStuff);
writer.write("\n");
writer.close();
You should probably be following the advice in the answer to the second link you posted. You can access the middle of a file using a random access file, but if you start appending at an arbitrary position in the middle of a file without recording what's there when you start writing, you'll be overwriting its current contents, as noted in this answer. Your best bet, unless the files in question are intractably large, is to assemble a new file using the existing file and your new data, as others have previously suggested.
AFAIK you cannot do that. I mean, appending a line is possible but not inserting in the middle. That has nothing to do with java or another language...a file is a sequence of written bytes...if you insert something in an arbitrary point that sequence is no longer valid and needs to be re-written.
So basically you have to create a function to do that read-insert-slice-rewrite
Hi I am writing some data to a text file through java code but when i again run the code its again appending to the older data ,i want the new data to overwrite the older version.
can any one help..
BufferedWriter out1 = new BufferedWriter(new FileWriter("inValues.txt" , true));
for(String key: inout.keySet())
{
String val = inout.get(key);
out1.write(key+" , "+val+"\n");
}
out1.close();
code would help, but its likely you are telling it to append the data since the default is to overwrite. find something like:
file = new FileWriter("outfile.txt", true);
and change it to
file = new FileWriter("outfile.txt", false);
or just
file = new FileWriter("outfile.txt");
since the default is to overwrite, either should work.
based on your edit just change the true to false, or remove it, in the FileWriter. The 2nd parameter is not required and when true specifies that you want to append data to the file.
You mentioned a problem of incomplete writes... BufferedWriter() isn't required, if your file is smallish then you can use FileWriter() by itself and avoid any such issues. If you do use BufferedWriter() you need to .flush() it before you .close() it.
BufferedWriter out1 = new BufferedWriter(new FileWriter("inValues.txt"));
for(String key: inout.keySet())
{
String val = inout.get(key);
out1.write(key+" , "+val+"\n");
}
out1.flush();
out1.close();
Set append parameter to false
new FileWriter(yourFileLocation,false);
You can use simple File and FileWriter Class.
The Constructor of FileWrite Class provides 2 different varieties to make a file. One which only takes the Object of file. and another is with two parameters one with file object and second is boolean true/false which indicates whether file to be created is going to be append the contents or overwriting.
following code will do the overwriting of content.
public class WriteFile {
public static void main(String[] args) throws IOException {
File file= new File("new.txt");
FileWriter fw=new FileWriter(file,true);
try {
fw.write("This is first line");
fw.write("This is second line");
fw.write("This is third line");
fw.write("This is fourth line");
fw.write("This is fifth line");
fw.write("hello");
} catch (Exception e) {
} finally {
fw.flush();
fw.close();
}
}
}
It works same with PrintWriter class also, since it also provides 2 different varieties of Constructors same as FileWriter. But you can always refer to Java Doc API.
I have a file (file.txt), and I need to empty his current content, and then to append some text multiple times.
Example: file.txt current content is:
aaa
bbb
ccc
I want to remove this content, and then to append the first time:
ddd
The second time:
eee
And so on...
I tried this:
// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.close();
// append
fileOut = new FileWriter("file.txt", true);
// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();
This works fine, but it seems inefficient because I open the file 2 times just for remove the current content.
When you open up the file to write it with your new text, it will overwrite whatever is in the file already.
A good way to do this is
// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.append("all your text");
fileOut.close();
The first answer is not correct. If you create a new filewriter with the true flag for the second parameter, it will open in append mode. This will cause any write(string) commands to "append" text to the end of the file, not wipe out whatever text is already there.
I'm just stupid.
I only needed to do this:
// empty the current content
fileOut = new FileWriter("file.txt");
// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();
And AT THE END close the stream.
I see that this question was answered quite a few Java versions ago...
Starting from Java 1.7 and using the new FileWriter + BufferWriter + PrintWriter for appending (as recommended in this SO answer ), my suggestion for file erasing and then appending:
FileWriter fw = new FileWriter(myFilePath); //this erases previous content
fw = new FileWriter(myFilePath, true); //this reopens file for appending
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("text");
//some code ...
pw.println("more text"); //appends more text
pw.flush();
pw.close();
Best I could think of is :
Files.newBufferedWriter(pathObject , StandardOpenOption.TRUNCATE_EXISTING);
and
Files.newInputStream(pathObject , StandardOpenOption.TRUNCATE_EXISTING);
In both the cases if the file specified in pathObject is writable, then that file will be truncated.
No need to call write() function. Above code is sufficient to empty/truncate a file.This is new in java 8.
Hope it Helps
I have a program that loads lines from a user file, then selects the last part of the String (which would be an int)
Here's the style it's saved in:
nameOfValue = 0
nameOfValue2 = 0
and so on. I have selected the value for sure - I debugged it by printing. I just can't seem to save it back in.
if(nameOfValue.equals(type)) {
System.out.println(nameOfValue+" equals "+type);
value.replace(value, Integer.toString(Integer.parseInt(value)+1));
}
How would I resave it? I've tried bufferedwriter but it just erases everything in the file.
My suggestion is, save all the contents of the original file (either in memory or in a temporary file; I'll do it in memory) and then write it again, including the modifications. I believe this would work:
public static void replaceSelected(File file, String type) throws IOException {
// we need to store all the lines
List<String> lines = new ArrayList<String>();
// first, read the file and store the changes
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while (line != null) {
if (line.startsWith(type)) {
String sValue = line.substring(line.indexOf('=')+1).trim();
int nValue = Integer.parseInt(sValue);
line = type + " = " + (nValue+1);
}
lines.add(line);
line = in.readLine();
}
in.close();
// now, write the file again with the changes
PrintWriter out = new PrintWriter(file);
for (String l : lines)
out.println(l);
out.close();
}
And you'd call the method like this, providing the File you want to modify and the name of the value you want to select:
replaceSelected(new File("test.txt"), "nameOfValue2");
I think most convenient way is:
Read text file line by line using BufferedReader
For each line find the int part using regular expression and replace
it with your new value.
Create a new file with the newly created text lines.
Delete source file and rename your new created file.
Please let me know if you need the Java program implemented above algorithm.
Hard to answer without the complete code...
Is value a string ? If so the replace will create a new string but you are not saving this string anywhere. Remember Strings in Java are immutable.
You say you use a BufferedWriter, did you flush and close it ? This is often a cause of values mysteriously disappearing when they should be there. This exactly why Java has a finally keyword.
Also difficult to answer without more details on your problem, what exactly are you trying to acheive ? There may be simpler ways to do this that are already there.