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();
请注意,在使用这些方法之前,需要处理可能发生的异常,并在完成后关闭文件流或清理资源。