Does Java actually have support for file creation. I've not been having a good time with I/O when a file doesn't exist... :(
Printable View
Does Java actually have support for file creation. I've not been having a good time with I/O when a file doesn't exist... :(
This might help you out. Be sure to import java.io.* or java.io.File into your app though. :p
Code:public static void create(){
// if directory already exists mkdir() will just return false and the directory
// will not be overwritten.
try{
File dir = new File("C:\\InitialDirectory");
dir.mkdir();
File f = new File("\\Justatextfile.txt");
f.createNewFile();
File dest = new File("C:\\InitialDirectory\\" + f.getName());
f.renameTo(dest);
}catch(IOException e){System.err.println(e);}
}
Ok. Easy. :)
I just kept getting the impression that the
File f = new File("\\Justatextfile.txt");
line was raising a FileNotFound exception...
The file dosn't actually have to be there in order to create a new file object. Certian streams will throw a FileNotFoundException if the if a nonexistent file is passed in as an arguement. Some classes like RandomAccessFile will throw a FileNotFoundException when in 'r' mode and the file does not exist. But will create a file if in 'rw'.
i think this works:
some of those class names amybe wrong but that close enuff 4 u 2 fig it out;)Code:import java.io.*;
PrintWriter file = new Printwriter ( new OutputSteamWriter ( new FileOutputStream ("asdf.txt")));