The fact that PrintWriter's method is called append() doesn't mean that it changes mode of the file being opened.
You need to open file in append mode as well:
PrintWriter pw = new PrintWriter(new FileOutputStream(
new File("persons.txt"),
true /* append = true */));
Also note that file will be written in system default encoding. It's not always desired and may cause interoperability problems, you may want to specify file encoding explicitly.
Answer from axtavt on Stack OverflowThe fact that PrintWriter's method is called append() doesn't mean that it changes mode of the file being opened.
You need to open file in append mode as well:
PrintWriter pw = new PrintWriter(new FileOutputStream(
new File("persons.txt"),
true /* append = true */));
Also note that file will be written in system default encoding. It's not always desired and may cause interoperability problems, you may want to specify file encoding explicitly.
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("persons.txt"),true));
The true is the append flag. See documentation.
How do you append data to an existing text file using java.io.PrintWriter?
[Java] Write to existing file without overwriting said text file
TBH, just open the file and handle the FileNotFoundException as file systems can and tend to be volatile meaning in between the test a filecan be created or be destroyed just after the test. However, the issue is FileWriter is creating the file so the else will almost always be true...
More on reddit.comAppending to a text file with FileWriter & bufferedWriter - Support - Kotlin Discussions
PrintWriter is clearing my data.txt file
Try using
FileWriter fileWriter = new FileWriter(fileName,true); //true here signifies append mode outputFile = new PrintWriter (fileWriter);More on reddit.com
Videos
so this is a part of my project that I'm struggling with. I need to be able to search if a file exists, and if it does then I could append to it without erasing the content of the original file. and if the file does not exist to make a new one. However I'm running into a problem in which when I enter a directory and a file name of a file that doesn't exist, it says it exists, and creates the file and adds data to it regardless of the fact not existing before running the program. What am I doing wrong here. I'm somewhat new to file writing
package writeToFile;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class writeTo {
public static void main(String[] args) throws Exception{
String fileDirectory;
String fileName;
System.out.println("Please enter the directory to create a new text file (Don't include file name or extension at the end i.e (C:\\Users\\JohnDoe\\Desktop)");
Scanner keyboard = new Scanner(System.in);
fileDirectory = keyboard.nextLine();
System.out.println("Please choose a file name for your text file (Don't Include .txt or any extensions at the end)");
fileName = keyboard.nextLine();
File newFile = new File(fileDirectory + "\\" + fileName +".txt");
FileWriter fileWrite = new FileWriter(newFile, true);
BufferedWriter bufferedWrite = new BufferedWriter(fileWrite);
PrintWriter write = new PrintWriter(bufferedWrite);
if(newFile.exists() == false){
newFile.createNewFile();
write.print("hello new worlkd");
System.out.println("File Created and written to sucessfully, Please check " + fileDirectory +" For the File " + fileName);
}
else{
System.out.println("The File " + fileName +" Already Exists, populating existing file");
write.print("hello old worlkd");
}
write.close();
keyboard.close();
}
}So i have made a file and it reads it properly with what I have provided. But when I add in the PrintWriter code it deletes all text in the data.txt file. Here is what I have written down.
public class files
{
public static void main () throws Exception
{
Scanner inputFile;
File fileName;
PrintWriter outputFile;
fileName = new File ("data.txt");
outputFile = new PrintWriter (fileName);
inputFile = new Scanner (fileName);
while (inputFile .hasNext())
{
//code
}
}
}
I have yet to add in a try/catch but I don't think that is why it wont work. I also tried slightly rearranging the order but that didn't work.
edit: Solved using
Scanner inputFile;
File fileName;
PrintWriter outputFile;
fileName = new File ("data.txt");
inputFile = new Scanner (fileName);
FileWriter fileWriter = new FileWriter(fileName,true); //true here signifies append mode
outputFile = new PrintWriter (fileWriter);