IO流的分类举例

2022/2/10 23:16:01

本文主要是介绍IO流的分类举例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、FileInputStream

方法

说明

int available()

返回可以从该输入流不阻塞地读取(或跳过)的字节数的估计值,该值可能为0,或当检测到流的结束时为0

int read()

向流中写一个字符

int read​(byte[] b)

将多个字节读到 b 数组中

int read​(byte[] b, int off, int len)

读取流中的多个字节并填充到数组中指定位置中

void reset()

重置流

log skip​(long n)

跳过多个字符

void close()

关闭流

public class TestFile {
    public static void main(String[] args) {
        //读取temp.txt文件内容
        //流使用后 一定要关闭!!!写在finally中
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            int temp = 0;
            while((temp = fis.read())!=-1) {
                System.out.println(temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}

read(byte[ ] b) : 读取内容到byte[ ]中,结果是读取字节的个数,如果没有读取到字节返回-1

public static void main(String[] args) {
        //读哪个文件
        FileInputStream fis = null;
        /*File file = new File("D:\\Lession\\Java2113\\temp.txt");
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            //读取一个字节,返回字节的unicode值
            //fis.read();
            //读取到byte[]中,返回读取的长度
            //fis.read(byte[] b);
            //读取到byte[]中,指定byte数组的位置
            //fis.read(byte[] b, int off, int len)
            /* System.out.println((char)fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read());
            System.out.println(fis.read()); //-1
            */ /*int temp = 0;
            while((temp=fis.read())!=-1) {
                System.out.println((char)temp);
            }*/
            //缓冲区
            byte[] bytes = new byte[10];
            int temp = 0;
            while((temp=fis.read(bytes))!=-1) {
                for(int i = 0;i<temp;i++) {
                    System.out.println((char)bytes[i]);
                }
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

 缓冲:如同生活中,将书从一个教室搬往另一个教室,若一本一本地去搬会很慢,也很累,例如int read()方法的使用;

而如果有一个书包作为搬运工具,将教室里的书装到书包中进行搬运会较快,有一个向书包中装书的缓冲过程,例如,int read​(byte[ ] b)方法的使用

读中文

public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            byte[] bytes = new byte[fis.available()];
            int temp = 0;
            if((temp=fis.read(bytes))!=-1) {
                System.out.println(new String(bytes,"UTF-8"));
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

二、FileOutputStream

方法

说明

void write​(byte b)

向流中写一个字节

void write​(byte[] str)

向流中写一个字节数组

void write​(byte[] b, int off, int len)

向流中写入字节数组的一部分

void flush()

刷新流

void close()

关闭流

构造器:

FileOutputStream(File file) 默认从开头新加内容,覆盖

FileOutputStream(File file,boolean append) 从末尾追加内容

public class TestFileOutputStream {
    public static void main(String[] args) {
        //向temp.txt中写 abcd
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\Lession\\Java2113\\temp.txt");
            fos.write("阿伯测的".getBytes());
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}

应用:将一个文件复制到另一个文件

 public static void copy1(String srcPath,String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte[] bytes = new byte[1024];
        try {
            fis = new FileInputStream(srcPath);
            fos = new FileOutputStream(destPath);
            int temp = 0;
            while ((temp = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

三、BufferedInputStream/BufferedOutputStream

•此流是一个处理流,是一个带缓冲区的字节输出流。 •BufferedOutputStream 可以减少IO次数。
public static void copy2(String srcPath,String destPath) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(srcPath);
            fos = new FileOutputStream(destPath);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos,1024);
            int temp = 0;
            while((temp=bis.read())!=-1) {
                //缓冲流 默认是缓冲区满时,向设备输出
                bos.write(temp);
            }
            //手动冲刷缓冲区向设备输出
            bos.flush();//冲刷,将最后不足1024的部分进行读取
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally {
            try {
                bis.close();
            } catch (IOException e1) {

                e1.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e1) {

                e1.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

最后需要flush一下!!!

四、FileReader和FileWriter

Reader和Writer的子类,都是字符流

public class TestFileReader {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("D:\\Lession\\Java2113\\temp1.txt");
            int temp = 0;
            char[] chars = new char[2];
            while((temp=fr.read(chars))!=-1) {
                for(char c:chars) {
                    System.out.println(c);
                }
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
public class TestFileWriter {
    public static void main(String[] args) {
    try {
        FileWriter fw = new FileWriter("D:\\Lession\\Java2113\\temp1.txt");
        fw.write("你好中国");
        fw.flush();
    } catch (IOException e) {

        e.printStackTrace();
        }
    }
}

五、BufferedReader 和 BufferedWriter

BufferedReader 提供了按行读的功能

BufferedWriter 提供了自定义缓冲区大小的功能

FileReader fr = null;
    try {
        fr = new FileReader("D:\\Lession\\Java2113\\temp1.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = null;
        while((str=br.readLine())!=null) {
            System.out.println(str);
        }
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
        
    try {
        FileWriter fw = new FileWriter("D:\\Lession\\Java2113\\temp1.txt");
        BufferedWriter br = new BufferedWriter(fw,1024);
        br.write("你好北京");
        br.flush();
    } catch (IOException e) {

        e.printStackTrace();
        }

六、InputStreamReader和OutputStreamWriter (转换流)

都是字符流 继承于Reader和Writer , 是字节流转成字符流的桥梁

public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113\\temp.txt");
            InputStreamReader reader = new InputStreamReader(fis,"UTF-8");
            int temp = 0;
            while((temp=reader.read())!=-1) {
                System.out.println((char)temp);
            }
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
            /*fos.write("你好北京".getBytes());*/
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            osw.write("你好北京");
            osw.flush();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

七、PrintWriter

public static void main(String[] args) {
        PrintWriter pw = null;
        try {
            FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
            pw = new PrintWriter(fos,true);
            pw.println("你好北京");
            pw.println("你好冬奥会");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
    }

八、DataInputStream和DataOutputStream

读写数据(基本类型数据)的字节流

try {
    FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113\\temp1.txt");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeInt(1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113\\temp1.txt");
        DataInputStream dis = new DataInputStream(fis);
        System.out.println(dis.readInt());
    } catch (IOException e) {

        e.printStackTrace();
    }
}



这篇关于IO流的分类举例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程