はじめに
この実験では、Java プログラミング言語を使ってファイルにデータを書き込む方法を学びます。プログラムで生成したデータを将来の使用のために保存する必要がある場合、ファイルにデータを書き込むことは非常に便利です。
この実験では、Java プログラミング言語を使ってファイルにデータを書き込む方法を学びます。プログラムで生成したデータを将来の使用のために保存する必要がある場合、ファイルにデータを書き込むことは非常に便利です。
最初のステップは、データを書き込む対象のファイルを開くか作成することです。~/project
ディレクトリに output.txt
という名前の新しいファイルを作成します。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path path = Paths.get("~/project/output.txt");
}
}
ファイルを開いたので、そのファイルに文字列を書き込むことができます。これは Files.write()
メソッドを使って行われます。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("~/project/output.txt");
String content = "This is some text that we want to write to the file";
Files.write(path, content.getBytes());
}
}
ここでは、文字列をバイトに変換して Files.write()
メソッドに渡すことで、ファイルに文字列を書き込んでいます。文字列をバイトに変換するには getBytes()
メソッドを使用する必要があることに注意してください。
上記のコードをターミナルで実行するには、まずターミナルでプロジェクトディレクトリに移動する必要があります。その後、次のコマンドを実行します:
javac Main.java && java Main
Files.write()
メソッドを使って、文字列のリストをファイルに書き込むこともできます。以下のようになります。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("~/project/output.txt");
List<String> lines = Arrays.asList("This is line 1", "This is line 2", "This is line 3");
Files.write(path, lines, StandardCharsets.UTF_8);
}
}
ここでは、文字列のリストをファイルに書き込んでいます。Files.write()
メソッドのオーバーロード版を使用しており、このメソッドは文字列のリストをパラメータとして受け取ります。
上記のコードを実行して文字列のリストをファイルに書き込むには、まずターミナルでプロジェクトディレクトリに移動する必要があります。その後、次のコマンドを実行します:
javac Main.java && java Main
既存のデータを上書きすることなくファイルにデータを書き込みたい場合は、ファイルを開く際に StandardOpenOption.APPEND
オプションを使用できます。これにより、新しいデータがファイルの末尾に追記されます。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.nio.file.StandardOpenOption;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("~/project/output.txt");
List<String> lines = Arrays.asList("This is line 4", "This is line 5", "This is line 6");
Files.write(path, lines, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
}
}
ここでは、Files.write()
メソッドに StandardOpenOption.APPEND
オプションを追加しています。これにより、Java に対して既存のデータを上書きするのではなく、新しいデータをファイルの末尾に追記するように指示しています。
追記操作を実行するには、まずターミナルでプロジェクトディレクトリに移動する必要があります。その後、次のコマンドを実行します:
javac Main.java && java Main
最後に、Files.close()
メソッドを使ってファイルを閉じる必要があります。これにより、バッファリングされたデータが閉じる前にファイルに書き込まれることが保証されます。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.nio.file.StandardOpenOption;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("~/project/output.txt");
String content = "This is some text that we want to write to the file";
Files.write(path, content.getBytes());
Files.write(path, System.lineSeparator().getBytes(), StandardOpenOption.APPEND);
Files.write(path, "This is a new line".getBytes(), StandardOpenOption.APPEND);
Files.close();
}
}
ただし、上記のコードでは Files.close()
の使用方法が正しくありません。Files
クラスの静的メソッドであるため、Files.close()
ではなく、path
オブジェクトの close
メソッドを呼び出す必要があります。修正後のコードは以下の通りです。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.nio.file.StandardOpenOption;
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("~/project/output.txt");
String content = "This is some text that we want to write to the file";
Files.write(path, content.getBytes());
Files.write(path, System.lineSeparator().getBytes(), StandardOpenOption.APPEND);
Files.write(path, "This is a new line".getBytes(), StandardOpenOption.APPEND);
path.close();
}
}
これにより、ファイルが適切に閉じられます。
この実験では、JavaのFiles
クラスを使ってファイルにデータを書き込む方法を学びました。文字列をファイルに書き込む方法、文字列のリストをファイルに書き込む方法、ファイルに追記する方法、およびファイルを閉じる方法を見てきました。これで、JavaのFiles
クラスの助けを借りて簡単にファイルを書き込めるようになったことを願っています。