IO流之-----FileReader FileWriter BufferedOutputStream BufferReader BufferedWriter

文件流

只能操作纯文本文件(记事本打开的.txt)
  字符型:FileReader
    1. java.io
    2. 继承InputStreamReader 再继承Reader
    3. 构造方法:
      有参带String
      有参带File
    4. 常用方法:
      read()
      read(char[])
      flush()

  1. 文件流的好处:
      每一行记录的信息都是相关的;
      我们可以把信息读取出来,直接看懂文件;
  2. 文件流的弊端:
      不安全
      只能记录String信息,不能记录一些动作(方法);
public static void main(String[] args) {
        File file = new File("D:\\test\\out11.txt");
        try {
            FileReader fr = new FileReader(file);
            char[] c = new char[100];
            int count = fr.read(c);
            while (count != -1){
                System.out.println(new String(c,0,count));
                count = fr.read(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
}  

字符流:

FileWriter
    1. java.io
    2. 继承OutputStreamWriter 再继承Writer
    3. 构造方法:
      有参带String 带String,boolean参数
      有参带File ,带File,boolean参数
    4. 常用方法:
      write(int)
      write(char[] )
      write(String)
      flush()
      close()
 字节型:BufferedInputStream
    1. java.io
    2. 继承FilterInputStream 再继承InputStream
    3. 创建对象:
      BufferedInputStream(InputStream in)
      BufferedInputStream(InputStream in,int size)
    4. 常用方法:
      int = available();
      close();
      int = read();
      int = read(byte[],int off,int len)
      long n = skip(long n)

public static void main(String[] args) {
        File file = new File("D:\\test\\out11.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            byte[] b = new byte[100];
            int count = bis.read(b);
            while (count != -1){
                System.out.println(new String(b,0,count));
                count = bis.read(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
}

字节流:

BufferedOutputStream
    1. java.io;
    2. 继承FilterOutputStream 再继承OutputStream
    3. 创建对象:
      BufferedOutputStream(OutputStream out)
      BufferedOutputStream(OutputStream out,int size)
    4. 常用方法:
      flush();
      write();
      write(byte[] b,int off,int len);

字符流:

BufferReader
    1. java.io
    2. 继承Reader类;
    3. 创建对象:
      BufferedReader(Reader in)
      BufferedReader(Reader in,int size)
    4. 常用方法:
      close()
      read()
      read(char[] c,int off,int len)
      readLine()
      long n = skip(long n)

public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("D:\\test\\out1.txt");
            BufferedReader br = new BufferedReader(fr);
            String s = br.readLine();
            while (s != null){
                System.out.println(s);
                s = br.readLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (fr !=null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
}

字符流:

BufferedWriter
    1. java.io
    2. 继承自Writer类;
    3. 创建对象:
      BufferedWriter(Writer out)
      BufferedWriter(Writer out,int size)
    4. 常用方法:
      close()
      flush()
      newLine()
      write(int c)
      write(char[] c,int off,int len)
      write(String s,int off, int len)

try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\test\\out1.txt",true));
            bw.newLine();
            bw.write("hyf---123",0,"hyf---123".length());
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

数组流

  • byte数组
      ByteArrayInputStream
      byteArrayOutputStream
  • char数组
      CharArrayReader
      CharArrayWriter

内存流

  • DataInputStream
  • DataOutputStream

对象流

对象流:将对象存入文件中。

  • ObjectInputStream
  • ObjectOutputStream
  1. 对象的序列化与反序列化:
      序列化:将一个完整的对象拆分成许多个字节,记录在文件中。
      反序列化:将文件中的对象字节拼接成一个完成的对象。
  2. 如果想把对象序列化到文件中
      需要让对象实现Serializable接口;
      同时为了让对象可以反序列化:
  3. 如果想要将对象反序列化
      需要让对象中存入一个属性:private long serialVersionUID = 任意L;

猜你喜欢

转载自blog.csdn.net/qq_40791843/article/details/90813793