If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.
As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
You should try creating a path for the folder it contains before:
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(file);
Answer from Jack on Stack OverflowIf the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.
As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
You should try creating a path for the folder it contains before:
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(file);
import java.io.PrintWriter;
import java.io.File;
public class Testing {
public static void main(String[] args) throws IOException {
File file = new File ("C:/Users/Me/Desktop/directory/file.txt");
PrintWriter printWriter = new PrintWriter ("file.txt");
printWriter.println ("hello");
printWriter.close ();
}
}
throw an exception for the file.
[Java]Writing to a large file, PrintWriter vs Files.write
java - how to use printwriter to create and write to a file - Stack Overflow
java - PrintWriter add text to file - Stack Overflow
Cannot overwrite a txt file, can only append to it
Videos
So, I'm reading from a db, and then writing to a file.
We have a large amount of records, so the file will become rather large. like 700mb - 1gb. Or more.
Right now I'm only playing with 1k records.
try(PrintWriter assetfilewriter = new PrintWriter(new BufferedWriter(new FileWriter(filePath1, false)))) {
for(int iter=0; iter<RSsize; iter++){
//do logic, get fields, save it to a string called logtxt
//Using Files.write
Files.write(Paths.get(filePath2), logtxt.getBytes(), StandardOpenOption.WRITE);
//Using PrintWriter
assetfilewriter.print(logtxt);
}
}Now when I sit and look at the output folder, the process takes around 10-20 seconds so I can see the files being updated.
filepath2, which is the Files.Write method, is constantly being updated.
filepath1, which is the PrintWriter method, remainds at 0kb untill apparently the loop finishes, then it writes to the file in one shot.
Which is the better method? Since we have a large amount of records, I don't really want java to store everything in memory and write it all at once. I've run into java memory exceptions a few times already.
the code works for me.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class NewClass {
void n() throws FileNotFoundException {
File Fileright = new File("/home/ubuntu/Documents/f.txt");
PrintWriter pw = new PrintWriter(Fileright);
for (int i = 0; i <= 3; i++) {
pw.println(i);
System.out.println(i);
}
pw.close();
}
public static void main(String[] args) throws FileNotFoundException {
new NewClass().n();
}
}
output:(in file: /home/ubuntu/Documents/f.txt)
0
1
2
3
FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
Please check the file Permission, you may use canRead() , canWrite() to check that, but it may not be sufficient enough.
Do this in order to create a PrinterWriter working with a FileWriter in append mode:
PrintWriter outFile = new PrintWriter(new FileWriter("planetaryData.txt", true));
From Mkyong's tutorials:
FileWriter, a character stream to write characters to file. By default, it will replace all the existing content with new content, however, when you specified a true (boolean) value as the second argument in FileWriter constructor, it will keep the existing content and append the new content in the end of the file.
You can use something like -
PrintWriter outputFile = new PrintWriter(new FileWriter(file, true));