欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javaIO流

程序员文章站 2022-04-04 09:37:40
...

1,InputStream 和 OutputStream继承结构图

javaIO流

2, Reader和Writer继承结构图

 

javaIO流

 

3.FileInputStream and FileOutputStream (字节输入流)  (抽象类)

public class Test {
    public static void main(String[] args) {

        OutputStream os = null;
        InputStream is = null;

        try {
            is = new FileInputStream("C:\\Users\\ly\\Desktop\\test.txt");
            os = new FileOutputStream("C:\\Users\\ly\\Desktop\\test_bak.txt");
            int b = 0;
            
            while((b=is.read()) != -1){
                os.write(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

4.FileReader and FileWriter (文件字符输入流)  (抽象类)

public class Test {
    public static void main(String[] args) {
        Reader r = null;
        Writer w = null;
        try {
            r = new FileReader("C:\\Users\\ly\\Desktop\\test.txt");
            w = new FileWriter("C:\\Users\\ly\\Desktop\\test_File.txt",true);

            int b = 0;
            while ((b = r.read()) != -1) {
                w.write(b);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (r != null) {

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

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

    }
}

5,缓冲流 (字节缓冲流 和 字符缓冲流)

1.缓冲流主要是为了提高效率而存在的,减少物理读取次数,缓冲流主要有:BufferedInputStream、 BufferedOutputStream、BufferedReader、BufferedWriter,

并且 BufferedReader 提供了实用方法readLine(),可以直接读取一行,BufferWriter 提供了 newLine()可以写换行符。

2.采用 BufferedInputStream对 InputStream进行装饰,BufferedInputStream 会将数据先读到缓存里,Java 程序再次读取数据时,直接到缓存中读取,减少IO次数

3.BufferedOutputStream对OutputStream进行装饰

 

public class Test {
    public static void main(String[] args) {

        InputStream is = null;
        OutputStream os = null;
        try {

            is = new BufferedInputStream(new FileInputStream("C:\\Users\\ly\\Desktop\\test.txt"));
            os = new BufferedOutputStream(new FileOutputStream("C:\\Users\\ly\\Desktop\\test_bak.txt",true));

            int b = 0;
            while((b=is.read()) != -1){
                os.write(b);
            }
            os.flush();
            System.out.println("文件复制完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{

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

            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class Test {
    public static void main(String[] args) {

        BufferedReader is = null;
        BufferedWriter os = null;
        try {

//            is = new BufferedReader(new FileReader("C:\\Users\\ly\\Desktop\\test.txt"));
//            os = new BufferedWriter(new FileWriter("C:\\Users\\ly\\Desktop\\test_bak.txt",true));
            is = new BufferedReader(new FileReader("C:\\Users\\ly\\Desktop\\test.txt"));

            os = new BufferedWriter(new FileWriter("C:\\Users\\ly\\Desktop\\test_bak.txt",true));
            String b=null;

            while((b=is.readLine()) != null){
                os.write(b);
            }
            os.flush();
            System.out.println("文件复制完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{

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

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

5 转换流

转换流主要有两个 InputStreamReader 和 OutputStreamWriter
 1 InputStreamReader 主要是将字节流输入流转换成字符输入流
 2 OutputStreamWriter 主要是将字节流输出流转换成字符输出流

public class Test {
    public static void main(String[] args) {
        BufferedReader is=null;
        BufferedWriter os=null;

        try {

            is = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\ly\\Desktop\\test.txt")));
            os = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\ly\\Desktop\\test_bak.txt",true)));

            String s=null;
            while((s=is.readLine()) != null){

                os.write(s);

            }

            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

6 打印流

打印流主要包含两个:PrintStream 和 PrintWriter,分别对应字节流和字符流

6.1屏幕打印重定向

public class Test {
    public static void main(String[] args) {
        OutputStream os=null;

        try {
            os = new FileOutputStream("C:\\Users\\ly\\Desktop\\console.txt");

            System.setOut(new PrintStream(os));
            System.out.println("你好世界");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally{
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

6.2 接受屏幕输入

import java.io.*;

public class Test {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(
                    new InputStreamReader(System.in));
            String s = null;
            while ((s = br.readLine()) != null) {
                System.out.println(s);
                //为 q 退出循环
                if ("q".equals(s)) {
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
            }
        }
    }
}

 

7 对象流

1.对象流可以将 Java 对象转换成二进制写入磁盘,这个过程通常叫做序列化,并且还可以从

磁盘读出完整的 Java 对象,而这个过程叫做反序列化。 对象流主要包括:ObjectInputStream 和 ObjectOutputStream

2.如果实现序列化该类必须实现序列化接口 java.io.Serializable,该接口没有任何方法,该接口只
 是一种标记接口,标记这个类是可以序列化的

3.不能序列化,对序列化的类是有要求的,这个序列化的类必须实现一个接口 Serializable,这个
 接口没有任何方法声明,它是一个标识接口,如:java 中的克隆接口 Cloneable,也是起到了一
 种标识性的作用

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关标签: Java