Java中写入文件的方法可以使用以下几种方式:
try {
FileWriter writer = new FileWriter("filename.txt");
writer.write("Hello, world!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt"));
writer.write("Hello, world!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
PrintWriter writer = new PrintWriter("filename.txt");
writer.println("Hello, world!");
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
这些方法都可以用于将数据写入到文件中。根据实际需求选择适合的方法。