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();
}
這些方法都可以用于將數據寫入到文件中。根據實際需求選擇適合的方法。