彩笔笔记2016/12/8——filewriter ,filereader

package p3;
import java.io.FileWriter;
import java.io.IOException;

public class TestFileWriter {
 public static void main(String[] args) throws Exception{
  //创建一个读文件的对象,传入的字符串是文件路径
  //如果文件不存在 则自动创建
  //布尔值表示是否继续向文件末尾追加内容如果是false则把文件原来的内容删除
  FileWriter fw = null;
  try{
   fw=new FileWriter("d:/java/homework/a.txt",true);
  
  //往流里写东西
  fw.write("hhd");
  int a=1/0;
  //把内存里面的东西刷到文件中
//  fw.flush();
  //关流
  }catch (IOException e){
   e.printStackTrace();
  }finally {
   try {
    if(fw!=null){
    fw.close();}
   } catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
   }
   
  }
 }
}

-----------------------------------


package p3;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class TestFileReader {
 public static void main(String[] args) {
   FileReader fr=null;
  try {
   fr=new FileReader("d:/java/homework/a.txt");
   //读取单个字符
/*   int result=0;
   while((result=fr.read())!=-1){
    System.out.print((char)result);    
   }*/
   char[] ch=new char[10];
   //read(ch)返回值是读取到的字符数
//   int len=fr.read(ch);
//   System.out.println(len);
//   System.out.println(new String(ch,0,len));
   int len;
   while((len=fr.read(ch))!=-1){
    System.out.println(new String(ch,0,len));
   }
   
  }catch(Exception e){
   e.printStackTrace();
  }finally {
   try {
    if(fr!=null){
     fr.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
   }
  }
  
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值