FileOutputStream fos = new FileOutputStream("file.txt");
String data = "Hello, World!";
byte[] bytes = data.getBytes();
fos.write(bytes);
fos.close();
BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"));
writer.write("Hello, World!");
writer.close();
PrintWriter writer = new PrintWriter("file.txt");
writer.println("Hello, World!");
writer.close();
String data = "Hello, World!";
Files.write(Paths.get("file.txt"), data.getBytes());
RandomAccessFile file = new RandomAccessFile("file.txt", "rw");
file.writeBytes("Hello, World!");
file.close();
請注意,在使用這些方法之前,需要處理可能發生的異常,并在完成后關閉文件流或清理資源。