4月27日学习总结——字符输入输出流、二进制

一、Reader类(字符读取)输入流

常用方法:抽象类
1.int  read()    //返回值是int类型
2.int read(char[] c)   //返回值是int类型,从输入流读取若干字符数据,并存储到字符数组
3.int read(char[] c,int off,int len)   //从输入流读取下标off开始至len结束字符数据,并存储到字符数组
4.void close()    //关闭资源 

、FileReader类 字符输入流)——>(读取)

FileReader类 InputStreamReader类子类,无法指定字符编码格式,方法通用,单个字符读取

构造方法

1.FileReader(File file);

2.FileReader(String path);  //字符串形式 文件路径
例:
Reader fr=null;
  try {
   fr=new FileReader("F:/好好.txt");
  } catch (Exception e) {
   
   e.printStackTrace();
  }
  
  int length=0;
  try {
   while ((length=fr.read())!=-1) { //循环读取文本里每一个字符转换成了ASCll码
    System.out.println((char)length); //读取接收到的数值强转为单个字符
    
   }
  } catch (IOException e) {
   e.printStackTrace();

}

三、InputStreamReader
InputStreamReader是Reader类子类,该类可以指定字符编码格式常用构造方法:

1.InputStreamReaderInputStream in); //传一个字节流,

2.InputStreamReaderInputStream in,String charSetName);字节流包装成字符流,后面指定编码格式
例:
  InputStream is=null;
  InputStreamReader isr=null;
  BufferedReader br=null;
  
  try {
   is=new FileInputStream("F:/好好.txt");  //将文件读取成字节流
   isr=new InputStreamReader(is,"GBK");//将字节流包装成字符流,后面指定编码格式,可为utf-8
   br=new BufferedReader(isr);    //把读到的字符流放到缓冲区
   
   String line;
   while ((line=br.readLine())!=null) { //.readLine方法是逐行读取缓冲区字符串
    System.out.println(line);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    br.close();
    isr.close();
    is.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  四、BufferedReader类(缓冲流)
.readLine() 方法 ,逐行读取字符串

实例:

Reader reader=null;
  BufferedReader br=null;
  try {
   reader=new FileReader("F:/好好.txt");  //引入文件 单个字符读取文件
   br=new BufferedReader(reader);   //读取每个字符到缓冲区
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  }
  String line=null;
  try {
   while ((line=br.readLine())!=null ) {//readLine()读取一行字符串
    System.out.println(line);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if (br!=null&&reader!=null) {
    try {
     br.close();
     reader.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
五、Writer类(字符输出流)——>(写入)

抽象类
常用方法:

1.writer(String);  //写入
2.close();   //关闭资源
3.flush();    //清空缓存,强制把流中的东西输出,写入到指定文件

六、FileWriter类
FileWriter类OutputStreamWriter类子类,无法指定字符编码格式,方法通用,每次单个字符写入
构造方法:
1.new FileWriter(File file)

2.new FileWriter(String path) //指定输出文件路径
例:

Writer fw=null;

  try {
   fw=new FileWriter("F:/陈京.txt",true); //后面带布尔true,写入到该文件是追加字符串
   String info="带你飞";
   fw.write(info);     
   fw.flush();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    fw.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
七、OutputStreamWriter
OutputStreamWriter类是Writer子类,可以指定字符编码格式,常用构造方法:
1.new OutputStreamWriter(OutputStream);
2.new OutputStream Writer( OutputStream,String charSetName);
八、BufferedWriter类

1. 带缓冲区的输出流
实例代码:

 Writer w=null;
  BufferedWriter bw=null;
  //将字符串写入文件
  try {
   w=new FileWriter("E:/浪.txt");   //指定写入目标文件
   bw=new BufferedWriter(w);     //包装成缓冲流,写入到文件
   String line="带你飞";   //赋值字符串,line接收
  
   bw.write(line);
   bw.flush();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if (bw!=null) {
    try {
     bw.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (w!=null) {
    try {
     w.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }

综合应用代码

1.替换文件
  FileInputStream is=null;   //字节流读取
  InputStreamReader ir=null; //字符流读取
  BufferedReader br =null;   //将读到的字符串包装成缓冲区
  FileWriter fw=null;     //写入到目标文件
  BufferedWriter bw=null; //将缓冲区的字符串写入到 bw
  
  try {
   is=new FileInputStream("E:/pet.template");
   ir=new InputStreamReader(is,"utf-8");
   br=new BufferedReader(ir);
   
   //字符串更改,不创建和回收,比String更高效
   StringBuffer sbf=new StringBuffer();//StringBuffer 字符串更改
   String line=null;
   while ((line=br.readLine())!=null) {
    sbf.append(line);   //.append 追加括号里的值
    
   }
   System.out.println("替换前:"+sbf);
   String newstr1=sbf.toString().replace("{name}", "欧欧"); //.replace 替换括号里的值,把前面的换位后面的
   String newstr2=newstr1.replace("{type}", "狗狗");
   String newstr3=newstr2.replace("{master}", "李伟");
   System.out.println("替换后:"+newstr3);
   
   fw=new FileWriter("F:/pet.txt");
   bw =new BufferedWriter(fw);
   bw.write(newstr3);
   bw.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
    try {
     if (bw!=null&&bw!=null&&br!=null&&ir!=null&&is!=null) {
      bw.close();
      fw.close();
      br.close();
      ir.close();
      is.close();
    }
    
   } catch (IOException e) {
    e.printStackTrace();
   }
   
  }
data 二进制文件读写(DataInputStream 与DatOutputStream
该方法是用于读取二进制文件(如音频,图片等都是二进制文件)

读写二进制文件实现步骤:(1)引入相关类     (2)创建流对象

                                                 (3)调用DataInputStream对象的read()方法读取数据

                                                 (4)调用DataOutputStream对象的write()方法写数据

                                                 (5)读取写入的数据   (6)关闭流对象

猜你喜欢

转载自blog.csdn.net/chenjingqi101/article/details/80114104