java ファイルを作成する
javaで、ファイルを作成する手順を記述してます。
環境
- OS windows11 home
- java 17.0.2
手順
ファイルを作成するには、「createNewFile」を使用します。
ファイル.createNewFile()
// 作成できれば true できなければ false が返ります
実際にカレントフォルダに「foo.txt」を作成してみます。
import java.io.*;
public class App {
public static void main(String[] args) throws Exception {
try {
File file = new File("./foo.txt");
if (file.createNewFile()) {
System.out.println("yes"); // yes
} else {
System.out.println("no");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
作成されていることが確認できます。
ファイルが既に存在する場合は「false」が返ります。
-
前の記事
GAS エラー「GoogleJsonResponseException: API call to people.people.getBatchGet failed with error: Quota exceeded for quota metric ‘Critical read requests (Contact and Profile Reads)’ and limit ‘Critical read requests (Contact and Profile Reads) per minute per user’ of service ‘people.googleapis.com’ for consumer ‘project_number:502114720403’.」が発生した場合の対処法 2023.12.20
-
次の記事
GAS スプレッドシートのセルの数式をR1C1形式で取得する 2023.12.22
コメントを書く