java中的File书写文件
在指定的E盘下创建一个TXT文件
String path = "E:/newworkspace/netResume/netCust.txt";
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write("hello Word!");
writer.newLine();// 换行输入
writer.write("This is the writeTest.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
writer.flush();
writer.close();
}
二、另一种方法
publicvoidTextToFile(finalString strFilename,finalString strBuffer)
{
try
{
// 创建文件对象
File fileText =newFile(strFilename);
// 向文件写入对象写入信息
FileWriter fileWriter =newFileWriter(fileText);
// 写文件
fileWriter.write(strBuffer);
// 关闭
fileWriter.close();
}
catch(IOException e)
{
//
e.printStackTrace();
}
}