码迷,mamicode.com
首页 > 编程语言 > 详细

java IO操作

时间:2015-09-07 22:26:03      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

java7中引入自动资源管理(ARM),使用try(){}catch{}finally{},()中的资源会在程序运行后自动释放。

 

文件流分为输入流和输出流,典型用法

FileReader fr = new FileReader("poem.txt");           //读取已有文件
FileWriter fw = new FileWriter("newPoem2.txt");   //向特定名文件写入(不存在先创建),覆盖
      
char[] cbuf = new char[32];                   //Reader和Writer读写以字符为单位
int hasRead = 0;              //返回一次读取数         
while ((hasRead = fr.read(cbuf)) > 0)
{
    fw.write(cbuf,0,hasRead);
}

FileInputStream fis = new FileInputStream("poem.txt");
FileOutputStream fos = new FileOutputStream("newPoem.txt"))
byte[] bbuf = new byte[32];                //InputStream和OutputStream读写以字节为单位
int hasRead = 0;
while ((hasRead = fis.read(bbuf)) > 0)
{
   fos.write(bbuf,0,hasRead);

}
               

 

java IO操作

标签:

原文地址:http://www.cnblogs.com/himanxu/p/4789911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!