/** |
* 以字符为单位写文件。 |
* @param fileName 文件名 |
*/ |
public static void writeFileByChars(String fileName){ |
File file = new File(fileName); |
Writer writer = null ; |
try { |
//打开文件输出流 |
writer = new OutputStreamWriter( new FileOutputStream(file)); |
String content = "文件内容:\n1,The First line;\n2,The second line." ; |
writer.write(content); |
System.out.println( "写文件" + file.getAbsolutePath() + "成功!" ); |
} catch (IOException e){ |
System.out.println( "写文件" + file.getAbsolutePath() + "失败!" ); |
e.printStackTrace(); |
} finally { |
if (writer != null ){ |
try { |
//关闭输出文件流 |
writer.close(); |
} catch (IOException e1) { |
} |
} |
} |
} |