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.
file io - Is there a way to use Java PrintWriter to write to a specific line? - Stack Overflow
[Java]Writing to a large file, PrintWriter vs Files.write
Common usage for File, Scanner, and PrintWriter classes
import java.io.PrintWriter problem. No txt file appearing in folder.
Videos
You can do this, but you have to do the bumping.
The simplest solution is to re-write the file with the extra line you need.
Or using RandomAccessFile, you can search for the third line, copy all the data in the file down, and insert the line you want to add.
No, PrintWriter can only overwrite a file or append to the end of it.
If you "overwrite" a line, the new line's length might differ from the original so all the content following the line would have to be shifted which PrintWriter cannot do.
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.