public void writeToFile(){
try {
PrintWriter out = new PrintWriter(new FileWriter("d:\\myfile\\aaa.txt" ));
out.println("hello");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
PrintWriter out = new PrintWriter(new FileWriter("d:\\myfile\\aaa.txt" ));
out.println("hello");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Java文件写入
本文介绍了一个简单的Java方法用于将字符串写入指定路径的文件中。该方法首先创建一个指向目标文件的PrintWriter对象,然后使用println方法写入文本,并通过flush确保所有数据被写入磁盘,最后关闭流。如果在写入过程中遇到IOException,则会打印堆栈跟踪。
719

被折叠的 条评论
为什么被折叠?



