LoginSignup
2
0

More than 5 years have passed since last update.

java ファイル作成

Last updated at Posted at 2018-06-28

javaでファイルを作成する際、FileクラスのcreateNewFileメソッドを使います。
File newfile = new File("/Users/Shared/java/ファイル名");

すでに同名のファイルが存在する場合、作成に失敗するのでfalseを返します.

サンプルコード

public class fileClass{
    public fileClass() throws IOException{

        File file = new File("/Users/Shared/java/java.txt");

        //createNewFileメソッドを使用してファイルを作成する
        if (file.createNewFile()){
            System.out.println("ファイル作成成功");
        }else{
            System.out.println("ファイル作成失敗");
        }
    }
}

指定先のディレクトリが見つからない場合、"IOException”の例外が発生するので注意が必要です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0