备份一下输出流,免得久不用忘记掉
2009-08-11 20:50
381 查看
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; public class WriteToFile { public static void writeFileByBytes(String fileName) { File file = new File(fileName); OutputStream out = null; try { // 打开文件输出流 out = new FileOutputStream(file); String content = "文件内容: 1,The First line; 2,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) { e1.printStackTrace(); } } } } public static void writeFileByChars(String fileName) { File file = new File(fileName); Writer writer = null; try { // 打开文件输出流 writer = new OutputStreamWriter(new FileOutputStream(file)); String counter = "文件内容 1,The First line; 2,The second line."; writer.write(counter); 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) { e1.printStackTrace(); } } } } public static void writeFileByLines(String fileName) { File file = new File(fileName); PrintWriter writer = null; try { writer = new PrintWriter(new FileOutputStream(file)); writer.println("文件内容:"); // 写字符串 // 写入各种基本类型数据 writer.print(true); writer.print(155); writer.println(); // 换行 writer.flush(); // 写入文件 System.out.println("写文件 " + file.getAbsolutePath() + "成功!"); } catch (IOException e) { System.out.println("写文件 " + file.getAbsolutePath() + "失败!"); e.printStackTrace(); } finally { if (writer != null) { writer.close(); // 关闭输出文件流 } } } public static void main(String[] args) { String fileName = "d://Test//newTemp.txt"; WriteToFile.writeFileByBytes(fileName); fileName = "d://Test//newTemp1.txt"; WriteToFile.writeFileByChars(fileName); fileName = "d://Test//newTemp2.txt"; WriteToFile.writeFileByLines(fileName); } }
其实这么重要的东西,应该死记在心里的。不过记的东西太多了。还是放在BLOGGER上好了。呵呵。。BLOGGER,小仓库。。^^
相关文章推荐
- java枚举类型 感慨一下,发现自己老了,一不用就忘记,所以有空就回顾一下
- mybatis 配置-简单记录一下,免得忘记了
- 工作中遇到sqlserver2008表跟表之间的数据备份,老容易忘记,在此做一下备份
- 关于c# 和 SQL SERVER 2000 日期比较查询的一点点经验,记录一下,免得忘记!
- 简单的SQL语句记一下,免得自己忘记
- 不用“维护计划”实现mssql定期备份并压缩存档
- cloustack系统虚拟机密码-备记一下免得忘了
- centos下mysql 最新版终于安装成功!备份一下几个关键地方
- mysql 记录, 长时间不用总忘记
- 在SQL语言中,join什么时候用,什么时候不用啊?请高手举例解释一下。谢谢
- 只需按一下空格键就可以标注 不用众里寻他千百度
- 复习一下sql server的差异备份
- 菜鸟刚开始接触redis,记录一下学习的过程,以免忘记
- 趁还没忘记写一下刚才“推荐组”会议的一些头绪
- 总结一下sql,免得每次都得去查看别人的东西(转)
- 执行sql命令,返回参数值,怕忘记了,记录一下
- 有时候久不用了就会忘记
- 做完了工作以来的第一个项目 总结了一下在这个项目中遇到的问题 备份一下
- 只学一点点:我的技术学习策略(虽然不赞同,但可以参考一下,针对不常用的技术可以这样,同时可以:寻找遁去的一,不用管别人怎么想;有学习的时间,不如自己写、自己实践,否则学完了都不知道是什么东西)
- 卸载wamp忘记备份MySql如何恢复MySql数据