package com.as400samplecode; import java.io.File; import java.io.IOException; public class CreateFile { public static void main(String[] args) { try { File myFile = new File("data/newFile.txt"); if (myFile.createNewFile()){ System.out.println("File is created!"); }else{ System.out.println("File already exists."); } } catch (IOException e) { e.printStackTrace(); } } }
All one can think and do in a short time is to think what one already knows and to do as one has always done!
How to create a file in Java if one doesn't exists
The File.createNewFile() method creates a new, empty file named by this abstract pathname if
and only if a file with this name does not yet exist. This methods return a true value if the file is created successfully and false if the file already exists or the operation failed.
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.