COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button
JAVA EXAMPLES NEW FILE CREATION
Java Examples - New File Creation
Problem Description:
How to create a new file?
Solution:
This example demonstrates the way of creating a new file by using File()
constructor and file.createNewFile() method of File class.
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try{
File file = new File("C:/myfile.txt");
if(file.createNewFile())
System.out.println("Success!");
else
System.out.println
("Error, file already exists.");
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
Result:
The above code sample will produce the following result (if "myfile.txt does not exist before)
Success!
No comments:
Post a Comment