Home > Java > javaTutorial > body text

Java Example - Create File

黄舟
Release: 2017-02-15 10:30:24
Original
1470 people have browsed it

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();
     }
   }
   }
Copy after login

The output result of the above code is:

文件创建成功!
Copy after login

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)!



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!