I want to create a .dat
file in java which doesn't exist. I don't know how to manually create it either. I know that the following code:
File f = new File(file);
is used for the file, but what exactly is the code for a file which doesn't exist. In other words create a new file.
A statement like File f = new File(file);
will not create a file on disk. Class java.io.File
only represents a file path, not the actual file on disk.
To create a new file, open a FileOutputStream
for it, which you can then use to write data to the file.
OutputStream out = new FileOutputStream("C:\\Temp\\filename.dat");
try {
// Write data to 'out'
} finally {
// Make sure to close the file when done
out.close();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With