Home  >  Article  >  Java  >  Java Example - Create File

Java Example - Create File

黄舟
黄舟Original
2017-02-15 10:30:241439browse

The following example demonstrates using the File() constructor and file.createNewFile() method of the File class to create a new file

/*
 author by w3cschool.cc
 Main.java
 */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("文件创建成功!");
         else
         System.out.println         ("出错了,改文件已经存在。");
     }
     catch(IOException ioe) {
        ioe.printStackTrace();
     }
   }
   }

The output result of the above code is:

文件创建成功!

The above is the Java example - the content of the created file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn