W3School TIY Editor

  • W3School 在线教程
  • 改变方向
  • 暗黑模式
​x
 
import java.io.File;
import java.io.IOException;
​
public class CreateFile {
  public static void main(String[] args) {
    try {
      File myObj = new File("filename.txt");
      if (myObj.createNewFile()) {
        System.out.println("文件已创建: " + myObj.getName());
      } else {
        System.out.println("文件已存在。");
      }
    } catch (IOException e) {
      System.out.println("发生错误。");
      e.printStackTrace();
    }
  }
}