Program to Create File using createNewFile() method in java file IO



java.io.File's createNewFile() creates file in path specified above,
Method return true - if file does not exist and was created successfully.
Method return false - if file file already exists.





Program to Create File using createNewFile() method in java file IO
/** JavaMadeSoEasy.com */
import java.io.File;
import java.io.IOException;
public class CreateFileTest {
   public static void main(String[] args) {
          try {
                 String fileName = "c:/myFile.txt";
                 File file = new File(fileName);
                 /*
                 * File's createNewFile() creates file in path specified above,
                 * Method return true - if file does not exist and was created successfully.
                 * Method return false - if file file already exists.
                 */
                 if (file.createNewFile())
                       System.out.println("File has been created successfully > "
                                                   +fileName);
                 else
                       System.out.println("File with the given name already exists > "
                                                   +fileName);
                
          } catch (IOException e) {
                 e.printStackTrace();
          }
      }
}
/*OUTPUT
File has been created successfully > c:/myFile.txt
*/


RELATED LINKS>

Find current directory or current path in java


Create File in current path using new File(currentPath, fileName), File.separator or System.getProperty("file.separator") in java file IO


Must read for you :

No comments:

Post a Comment