Files and I/O (Cont.)
Creating a File
Use the createNewFile()
method to create a file in a specific directory (requires permission) by specifying the path of the file and use double backslashes to escape the "\"
character (for Windows):
File myObj = new File( "C:\\Users\\MyName\\filename.txt" );
On Mac and Linux you can just write the path, like: /users/name/filename.txt
.
This method returns a boolean
value: true
if the file was successfully created, and false
if the file already exists.
Note that the method is enclosed in a try...catch
block.
This is necessary because it throws an IOException
if an error occurs (if the file cannot be created for some reason):
Never argue with a fool, they will lower you to their level, and then beat you with experience.
|