IO输入/输出流(三)

2018-07-15  本文已影响0人  菜鸟要逆袭

前言:

Java中对数据进行持久化操作

转换流:

字节流与字符流之间转换的桥梁,可以用于改变字符的编码格式,编码统一,解决乱码问题

public class OutputStreamWriterDemo throws IOException{
  public static void main(String[] args){
    writeGBK();//UTF-8编码写入方法相同
  }
  //使用GBK编码形式写入
  public static void writeGBK() throws IOException{
    //创建字节输出流
    FileOutputStream fos = new FileOutputStream("c"\\gbk.txt");
    //创建转换流对象,构造方法,绑定字节输出流,指定GBK编码格式
    OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
    //转换流写入数据
    osw.write("你好");  
    osw.flush();
    osw.close();
  }
}
public class InputStreamReaderDemo throws IOException{
  public static void main(String[] args){
    readGBK();//UTF-8编码读取方法相同
  }
  //使用GBK编码形式读取
  public static void readGBK() throws IOException{
    //创建字节输出流
    FileInputStream fis = new FileInputStream("c"\\gbk.txt");
    //创建转换流对象,构造方法,绑定字节输入流,指定GBK编码格式
    InputStreamReader isr = new InputStreamReader(fis,"GBK");
    int len = 0;
    char[] ch = new char[1024];
    while((len = isr.read(ch)) != -1){
      System.out.println(new String(len));
    }
    isr.close();
  }
}

缓冲流:

内部包含了缓冲区,提高了IO流读写效率

public class BufferedOutputStreamDemo{
  public static void main(String[] args) throws IOException{
    //创建字节输出流,绑定文件
    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    //创建字节输出流缓冲流的对象,构造方法中,传递字节输出流(OutputStream的子类)
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    //写入字节和字节数组
    bos.write("65");//显示"A"
    bos.write("hello".getBytes());//显示"Ahello"
    //关闭缓冲流
    bos.close();
  }
}
public class BufferedIntputStreamDemo{
  public static void main(String[] args) throws IOException{
    //创建字节输入流,绑定文件
    FileInputStream fis = new FileInputStream("c:\\test.txt");
    //创建字节输入流缓冲流的对象,构造方法中,传递字节输入流(InputStream的子类)
    BufferedOutputStream bis = new BufferedOutputStream(fis);
    //读取字节
    byte[] bytes = new byte[1024];
    int len = 0;
    while((len = bis.read(bytes)) != -1){
      System.out.println(new String(bytes,0,len));
    }
    //关闭缓冲流
    bis.close();
  }
}
public class Copy{
  /*
  * 按效率排名:缓冲流读字节数组>字节流读字节数组>缓冲流读单个字节>字节流读单个字节
  */

  //使用字节流,读取单个字节
  public static void copy_1(File address1,File address2){
    FileInputStream fis = new FileInputStream(address1);
    FileOutputStream fos = new FileOutputStream(address2);
    int len = 0;
    while((len = fis.read()) != -1){
      fos.write(len);
    }
    fos.close();
    fis.close();
  }
  //使用字节流,读取字节数组
  public static void copy_2(File address1,File address2){
    FileInputStream fis = new FileInputStream(address1);
    FileOutputStream fos = new FileOutputStream(address2);
    int len = 0;
    byte[] bytes = new bytes[1024]
    while((len = fis.read()) != -1){
      fos.write(bytes,0,len);
    }
    fos.close();
    fis.close();
  }
  //使用缓冲字节流,读取单个字节
  public static void copy_3(File address1,File address2){
    BufferedInputStream bis = new BufferedInputStream(address1);
    BufferedOutputStream bos = new BufferedOutputStream(address2);
    int len = 0;
    while((len = fis.read()) != -1){
      fos.write(len);
    }
    bos.close();
    bis.close();
  }
  //使用缓冲字节流,读取字节数组
  public static void copy_3(File address1,File address2){
    BufferedInputStream bis = new BufferedInputStream(address1);
    BufferedOutputStream bos = new BufferedOutputStream(address2);
    int len = 0;
    byte[] bytes = new bytes[1024]
    while((len = fis.read()) != -1){
      fos.write(bytes,0,len);
    }
    bos.close();
    bis.close();
  }
}
public class WriterDemo{
  public static void main(String[] args){
    //创建字符输出流,封装文件
    FileWriter fw = new FileWriter("c:\\test.txt");
    //创建字符输出流缓冲流的对象,构造方法中,传递字符输出流(Writer的子类)
    BufferedWriter bw = new BufferedWriter(fw);
    //写入字符
    bw.write(100);
    bw.flush();
    bw.newLine();//换行的作用,BufferedWriter特有的方法,同"\r\n"作用相同,但其具有跨平台型,不用考虑操作系统中的换行符
    bw.write("你好");
    bw.flush();//显示"d你好"
  }
}
public class ReaderDemo{
  public static void main(String[] args){
    //创建字符输入流,封装文件
    FileReader fr = new FileReader("c:\\test.txt");
    //创建字符输入流缓冲流的对象,构造方法中,传递字符输入流(Reader的子类)
    BufferedReader br = new BufferedReader(fr);
    //读取字符
    String line = null; 
    while((line = br.readLine() != null){
      //readLine()是BufferedReader的特有方法,读取一行的字符,但不包括换行符
      System.out.println(line);
    }
    br.close();
  }
}
public class Copy{
  public static void main(String[] args){
    BufferedReader br = new BufferedReader("c:\\test.txt");
    BufferedWriter bw = new BufferedWriter("d:\\test.txt");
    String line = null;
    while((br.readLine()) != null){
      bw.write(line);
      bw.newLine();
      bw.flush();
    }
    bw.close();
    br.close();
  }
}

IO流的操作规律:

上一篇 下一篇

猜你喜欢

热点阅读