java IO流的总结

 //缓冲区为字符串
    public void test4(){
  BufferedReader bufr = null;
  BufferedWriter bufw = null;
  try {
   bufr = new BufferedReader(new FileReader("F:\\hello.java"));
   bufw = new BufferedWriter(new FileWriter("F:\\demo_copy.txt"));
   String line = null;
   while((line = bufr.readLine())!=null){
    bufw.write(line);
    bufw.newLine();//换行
    bufw.flush();
   }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally{
    try {
     if(bufr != null)
      bufr.close();
     if(bufw != null)
      bufw.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
  }
 }
    //缓冲区为字符数组
    public void test5(){
     BufferedReader bufr = null;
  BufferedWriter bufw = null;
  try {
   bufr = new BufferedReader(new FileReader("F:\\hello.java"));
   bufw = new BufferedWriter(new FileWriter("F:\\demo_copy1.txt"));
   char[] ch = new char[1024];
   int  len = 0;
   while((len = bufr.read(ch))!= -1){
    bufw.write(ch,0,len);
    bufw.flush();
   }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally{
    try {
     if(bufr != null)
      bufr.close();
     if(bufw != null)
      bufw.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
  }
    }
    //解析单个字符
    public void test6(){
     BufferedReader bufr = null;
  BufferedWriter bufw = null;
  try {
   bufr = new BufferedReader(new FileReader("F:\\hello.java"));
   bufw = new BufferedWriter(new FileWriter("F:\\demo_copy1.txt"));
   int  ch = 0;
   while((ch = bufr.read())!= -1){
    bufw.write(ch);
   }
   bufw.flush();
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }finally{
    try {
     if(bufr != null)
      bufr.close();
     if(bufw != null)
      bufw.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
  }
    }
   
    public void test7(){
     
     String str = "{\"status\":\"200\",\"info\":\"1\"}";
     
     JSONObject json = JSONObject.fromObject(str);
     
     System.out.println(json);
    }
    //InputStream OutputStream
    public void test8(){
     BufferedInputStream bfis = null;
     BufferedOutputStream bfos  =null;
     try {
      bfos = new BufferedOutputStream(new FileOutputStream("F:\\demo3.ppt"));
   bfis = new BufferedInputStream(new FileInputStream("F:\\STAU2.ppt"));
   byte[] by = new byte[1024];//使用数组进行缓冲
   int len = 0;
   while ((len = bfis.read(by)) != -1) {
    bfos.write(by, 0, len);
    bfos.flush();
   }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch (Exception e) {
   try {
    System.setOut(new PrintStream("f:\\a.log"));//改变标准输出设备,System.out默认在控制台上
    e.printStackTrace(System.out);
   } catch (IOException e2) {
    // TODO: handle exception
   }
  }finally{
   try{
    if(bfos != null)
     bfos.close();
    if(bfis != null)
     bfis.close();
   }catch(IOException io){
    io.printStackTrace();
   }
  }
    }
   
    //转换流
    public void test9(){
     //InputStream inputStream = System.in;
     //InputStreamReader isr = new InputStreamReader(inputStream);
     
     //转换流中可以指定编码表
     //InputStreamReader isr = new InputStreamReader(inputStream,"utf-8");
     BufferedReader bfr = null;
     BufferedWriter bfw = null;
     try {
      String line = null;
      bfr = new BufferedReader(new InputStreamReader(System.in));
      bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:\\b.txt")));
   while((line = bfr.readLine())!= null){
    bfw.write(line);
    bfw.flush();
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try{
    if(bfr != null)
     bfr.close();
    if(bfw != null)
     bfw.close();
   }catch(IOException io){
    io.printStackTrace();
   }
  }
    }
    public void test10(){
     Properties prop = new Properties();
     try {
      BufferedWriter bfw =new BufferedWriter(new FileWriter("F:\\hello.java"));
   prop.load(new FileInputStream("F:\\v.txt"));
   System.out.println("========"+prop.size());
   bfw.write(prop.toString());
   bfw.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值