Different ways of creating files
There are methods for creating files in java.io.File
(like createNewFile()
or mkdir()
). Are there other ways of creating files in Java SE using "st开发者_Go百科andard" API?
When you create a FileOuputStream
, the file is created, if it does not exist, although this is not guaranteed:
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform.
FileOutputStream
can be used to create a file as shown below
FileOutputStream fos = new FileOutputStream("myfile");
FileOutputStream fos = new FileOutputStream(new File("myfile"));
You can use PrintWriter in conjunction with FileWriter such as PrintWriter write = new PrintWriter(new FileWriter("FileName", false)); writing to blank file or BufferedWriter works with FileWriter as well, such as BufferedWriter writer = new BufferedWriter(new FileWriter("FileName"));
精彩评论