/** |
* 以字节为单位写文件。适合于写二进制文件。如图片等 |
* @param fileName 文件名 |
*/ |
public static void writeFileByBytes(String fileName){ |
File file = new File(fileName); |
OutputStream out= null ; |
try { |
// 打开文件输出流 |
out = new FileOutputStream(file); |
String content = "文件内容:\n1,The First line;\n2,The second line." ; |
byte [] bytes = content.getBytes(); |
//写入文件 |
out.write(bytes); |
System.out.println( "写文件" + file.getAbsolutePath() + "成功!" ); |
} catch (IOException e){ |
System.out.println( "写文件" + file.getAbsolutePath() + "失败!" ); |
e.printStackTrace(); |
} finally { |
if (out != null ){ |
try { |
//关闭输出文件流 |
out.close(); |
} catch (IOException e1) { |
} |
} |
} |
} |