文件字节流、字符流、缓冲字节流、缓冲字符流、数据流

1、字节流(通常与缓冲字节流一起使用,提升效率。具体参考3)
FileOutputStream 输出流,写出内容到文件
FileInputStream 输入流,从文件读入内容到缓冲区
public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");//指定路径、文件(夹)名
        try {//异常处理
            FileOutputStream out = new FileOutputStream(f, true);//默认不追加,而是覆盖。
            String str = "与众不同";
            byte b[] = str.getBytes();//字符串转字节
            out.write(b);
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        try {
            FileInputStream in = new FileInputStream(f);
            byte b2[] = new byte[200];//设定缓冲区大小,200字节
            int length = in.read(b2);//读入文本到缓冲区,返回文本字节数。
            System.out.println("文件内容是:" + new String(b2, 0, length));//0,length可以去除空格(未使用缓冲区)
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

 2、字符流(通常与缓冲字符流一起使用,提升效率。具体参考4)。直接对字符进行处理,无需字符/字节转换。

FileWriter
FileReader
public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(f, true);//默认覆盖
            String str = "为所欲为";
            fw.write(str);//写入文本
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭流
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileReader fr = null;
        try {
            fr = new FileReader(f);
            char ch[] = new char[1024];//缓冲区。由于是字符输入输出流,所以返回字符型
            int length = fr.read(ch);//读取文本到缓冲区。返回文本长度
            System.out.println("文件内容是:" + new String(ch, 0, length));//消除空格(多余缓冲区)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 3、缓冲字节流

BufferedOutputStream

BufferedInputStream

缓冲字节流可以提高效率。字节流可以认为是一个货物一个货物地运输,而缓冲字节流可以把很多货物存放到货车上(缓存),一起运送。

使用缓冲字节输出流时,推荐多使用flush。刷新可以不必等货车装满就可以输送。

public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");
        FileOutputStream out = null;
        BufferedOutputStream bOut = null;
        try {
            out = new FileOutputStream(f, true);//默认覆盖
            bOut = new BufferedOutputStream(out);
            String str = "为所欲为";
            byte b[] = str.getBytes();//字符转字节
            bOut.write(b);//写入字节
            //使用缓冲字节输出流时,推荐多使用flush
            bOut.flush();//刷新
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//关闭流
            if (bOut != null) {
                try {
                    bOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileInputStream in = null;
        BufferedInputStream bIn = null;
        try {
            in = new FileInputStream(f);
            bIn = new BufferedInputStream(in);
            byte b[] = new byte[1024];
            try {
                int length=bIn.read(b);
                System.out.println("文件内容是:"+new String(b,0,length));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (bIn != null) {
                try {
                    bIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 4、缓冲字符流(除了read/write功能,还有按行处理的功能。如newLine,readLine。按行处理,不需要指定缓冲区大小)

BufferedWriter

BufferedReader

public class Demo {
    public static void main(String[] args) {
        File f = new File("word.txt");
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(f);//字符流
            bw = new BufferedWriter(fw);//缓冲字符流,提升效率
            String str1 = "这是第一行";
            String str2 = "这是第二行";
            bw.write(str1);
            bw.newLine();//创建新行
            bw.write(str2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();//注意关闭顺序,先关闭后创建的
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();//关闭最初创建的
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(f);//字符流
            br = new BufferedReader(fr);//缓冲字符流,可以按行读取
            String tmp = null;
            while ((tmp = br.readLine()) != null) {//br.readLine()只能读取第一行,while遍历读取所有行
                System.out.println(tmp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 5、数据流

DataOutputStream

DataInputStream

public class Demo {
    public static void main(String[] args) {
        File f = new File("word.txt");
        FileOutputStream out = null;
        DataOutputStream dos = null;
        try {
            out = new FileOutputStream(f);
            dos = new DataOutputStream(out);
            //word.txt中部分内容乱码
            dos.writeUTF("写入字符串数据");
            dos.writeInt(520);//整型
            dos.writeDouble(3.14f);
            dos.writeBoolean(true);//布尔型
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileInputStream in = null;
        DataInputStream dis = null;
        try {
            in = new FileInputStream(f);
            dis = new DataInputStream(in);
            //注意读取顺序,与写入顺序一致,否则错误。
            System.out.println(dis.readUTF());
            System.out.println(dis.readInt());
            System.out.println(dis.readDouble());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/xixixing/p/9517312.html