java-io初步:Reader、Writer、Buf…

java四个基本的io基类:

字节流:InputStream、OutputStream

字符流:Reader、Writer.——作为硬盘的文件使用的两个子类:FileWriter、FileReader.

 

1、java早期只有字节流,后边由于处理不同字符的编码需求,出现了字符流.

2、java io体系中子类的命名通常都是以父类的名称作为后缀.

 

 

import java.io.*;
class IODemo
{
 public static void main(String[] args) // throws Exception
 {
  //fileWrite();
  //fileRead();
  //bufferdWrite();
  //bufferdRead();

  //通过缓冲区拷贝一个.java文件
  copyJavaFile();
 }

 public static void fileWrite()
 {
  FileWriter fw = null;  //fw.close()调用需要引用fw.
  try
  {
   fw = new FileWriter("iodemo.txt", false); //第二个参数决定是否向覆盖原文件.
   fw.write("sddddddd");   
   fw.write("\r\naaa"); //windows\r\n为一个回车符.  
  }
  catch (Exception e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    fw.close();
   }
   catch (Exception e)
   {
    System.out.println(e.toString());
   }
  }
 }

 public static void fileRead()
 {
  //读取文件通用格式:
  //建立字符流读取对象,于指定文件关联
  FileReader fr = null;
  try
  {
   fr = new FileReader("iodemo.txt");
   char[] arr = new char[1024];  //将读出的数据存入到数组中.(缓存)
   int num = 0;
   while ((num = fr.read(arr)) != -1) //没有读到数据后,返回-1,循环结束;read方法可以自动向后读取剩下的数据,
   {
    System.out.println(new String(arr, 0, num));
   }
  }


  catch (Exception e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    fr.close();
   }
   catch (Exception e)
   {
    System.out.println(e.toString());
   }
  }
 }

 public static void bufferdWrite()
 {
  //创建流对象  
  FileWriter fw = null;
  //创建缓冲区跟流对象关联。
  BufferedWriter bfdw = null;
  try
  {
   fw = new FileWriter("iodemo.txt");
   bfdw = new BufferedWriter(fw);
   int i = 0;
   while (i < 5)
   {
    bfdw.write("aaa" + i);
    bfdw.newLine(); 
    bfdw.flush();
    i++;
   }

   bfdw.write("xxx"); //不调用flush或close,这一行数据不能写入.
   bfdw.newLine();
  }
  catch (Exception e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    bfdw.close();  //缓冲区的关闭是调用传入流的关闭方法.
   }
   catch (Exception e)
   {
    System.out.println(e.toString());
   }
  }
 }

 public static void bufferdRead()
 {
  //创建流对象
  FileReader fr = null;
  //创建缓冲区跟流对象关联。
  BufferedReader bfdr = null;
  try
  {
   fr = new FileReader("iodemo.txt");    
   bfdr = new BufferedReader(fr);
   String s;
   while ( (s = bfdr.readLine()) != null)
   {
    System.out.println(s);
   }
  }
  catch (Exception e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   try
   {
    bfdr.close();  //缓冲区的关闭是调用传入流的关闭方法.
   }
   catch (Exception e)
   {
    System.out.println(e.toString());
   }
  }
 }

 public static void copyJavaFile()
 {
  //创建流对象
  FileReader fr = null;
  FileWriter fw = null;
  //创建缓冲区跟流对象关联。
  BufferedReader bfdr = null;
  BufferedWriter bfdw = null;
  try
  {
   fr = new FileReader("iodemo.java");
   fw = new FileWriter("iodemo复件.txt");
   bfdr = new BufferedReader(fr);
   bfdw = new BufferedWriter(fw);
   String line;
   while ( (line = bfdr.readLine()) != null)  //readLine没有读取回车符,只读取了回车符前的数据,因此写入文本时需要执行newLine方法.
   {
    bfdw.write(line);
    bfdw.newLine();
    bfdw.flush();
    //System.out.println(line);
   }
  }
  catch (IOException e)
  {
   System.out.println(e.toString());
  }
  finally
  {
   //对两个缓冲区分别进行关闭
   if (bfdw != null)
   {
    try
    {
     bfdw.close();    
    }
    catch (Exception e)
    {
     System.out.println(e.toString());
    }
   }
   
   if (bfdr != null)
   {
    try
    {
     bfdr.close();    
    }
    catch (Exception e)
    {
     System.out.println(e.toString());
    }
   }
  }
 }
}

转载于:https://my.oschina.net/dendy/blog/385562

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值