美文网首页
Java基础之IO流

Java基础之IO流

作者: 从小白到全栈 | 来源:发表于2018-08-22 15:07 被阅读0次

流就是当不同的介质之间有数据交互的时候,JAVA就使用流来实现。数据源可以是文件,还可以是数据库,网络甚至是其他的程序,比如读取文件的数据到程序中,站在程序的角度来看,就叫做输入流

字节流:

字节输入流: InputStream

字节输出流:OutputStream

以字节流的方式读取文件:

public class TestStream {

    public static void main(String[] args) {

        try {

     //准备文件lol.txt其中的内容是AB,对应的ASCII分别是65 66

            File f =new File("d:/lol.txt");

            //创建基于文件的输入流

            FileInputStream fis =new FileInputStream(f);

            //创建字节数组,其长度就是文件的长度

            byte[] all =new byte[(int) f.length()];

            //以字节流的形式读取文件所有内容

            fis.read(all);

            for (byte b : all) {

                //打印出来是65 66

                System.out.println(b);

            } 

            //每次使用完流,都应该进行关闭

            fis.close(); 

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }  

    }

}

以字节流的方式向文件写数据:

public class TestStream {

    public static void main(String[] args) {

        try {

            // 准备文件lol2.txt其中的内容是空的

            File f = new File("d:/lol2.txt");

  // 准备长度是2的字节数组,用88,89初始化,其对应的字符分别是X,Y

            byte data[] = { 88, 89 };

            // 创建基于文件的输出流

            FileOutputStream fos = new FileOutputStream(f);

            // 把数据写入到输出流

            fos.write(data);

            // 关闭输出流

            fos.close();         

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

流的关闭方式有3种:1、try中关闭 2、finally中关闭 3、使用try()方式关闭

1、在try的作用域里关闭文件输入流,一般在开发中经常使用,这样做有一个弊端;

如果文件不存在,或者读取的时候出现问题而抛出异常,那么就不会执行这一行关闭流的代码,存在巨大的资源占用隐患。 不推荐使用

public class TestStream {

    public static void main(String[] args) {

        try {

            File f = new File("d:/lol.txt");

            FileInputStream fis = new FileInputStream(f);

            byte[] all = new byte[(int) f.length()];

            fis.read(all);

            for (byte b : all) {

                System.out.println(b);

            }

            // 在try 里关闭流

            fis.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

2、这是标准的关闭流的方式

(1)首先把流的引用声明在try的外面,如果声明在try里面,其作用域无法抵达finally.

(2)在finally关闭之前,要先判断该引用是否为空

(3)关闭的时候,需要再一次进行try catch处理

这是标准的严谨的关闭流的方式,但是看上去很繁琐,所以写不重要的或者测试代码的时候,都会采用上面的有隐患try的方式,因为不麻烦

public class TestStream {

    public static void main(String[] args) {

        File f = new File("d:/lol.txt");

        FileInputStream fis = null;

        try {

            fis = new FileInputStream(f);

            byte[] all = new byte[(int) f.length()];

            fis.read(all);

            for (byte b : all) {

                System.out.println(b);

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            // 在finally 里关闭流

            if (null != fis)

                try {

                    fis.close();

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

        }

    }

}

3、把流定义在try()里,try,catch或者finally结束的时候,会自动关闭,这种编写代码的方式叫做 try-with-resources, 这是从JDK7开始支持的技术,所有的流,都实现了一个接口叫做 AutoCloseable,任何类实现了这个接口,都可以在try()中进行实例化。并且在try, catch, finally结束的时候自动关闭,回收相关资源。

public class TestStream {

    public static void main(String[] args) {

        File f = new File("d:/lol.txt");

        //把流定义在try()里,try,catch或者finally结束的时候,会自动关闭

        try (FileInputStream fis = new FileInputStream(f)) {

            byte[] all = new byte[(int) f.length()];

            fis.read(all);

            for (byte b : all) {

                System.out.println(b);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

字符流:

字符输入流:Reader

字符输出流:Writer

使用字符流读取文件:

public class TestStream {

    public static void main(String[] args) {

        // 准备文件lol.txt其中的内容是AB

        File f = new File("d:/lol.txt");

        // 创建基于文件的Reader

        try (FileReader fr = new FileReader(f)) {

            // 创建字符数组,其长度就是文件的长度

            char[] all = new char[(int) f.length()];

            // 以字符流的形式读取文件所有内容

            fr.read(all);

            for (char b : all) {

                // 打印出来是A B

                System.out.println(b);

            }

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

使用字符流把字符串写入到文件:

public class TestStream {

    public static void main(String[] args) {

        // 准备文件lol2.txt

        File f = new File("d:/lol2.txt");

        // 创建基于文件的Writer

        try (FileWriter fr = new FileWriter(f)) {

            // 以字符流的形式把数据写入到文件中

            String data="abcdefg1234567890";

            char[] cs = data.toCharArray();

            fr.write(cs);

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

缓存流:

缓存字符输入流BufferedReader, 可以一次读取一行数据

缓存字符输出流PrintWriter , 可以一次写出一行数据

使用缓存字符输入流读取文件:

public class TestStream {

    public static void main(String[] args) {

        // 准备文件lol.txt其中的内容是

        // garen kill teemo

        // teemo revive after 1 minutes

        // teemo try to garen, but killed again

        File f = new File("d:/lol.txt");

        // 创建文件字符流

        // 缓存流必须建立在一个存在的流的基础上

        try (

                FileReader fr = new FileReader(f);

                BufferedReader br = new BufferedReader(fr);

            )

        {

            while (true) {

                // 一次读一行

                String line = br.readLine();

                if (null == line)

                    break;

                System.out.println(line);

            }

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

使用缓存流写出数据:

public class TestStream {

    public static void main(String[] args) {

        // 向文件lol2.txt中写入三行语句

        File f = new File("d:/lol2.txt");

        try (

                // 创建文件字符流

                FileWriter fw = new FileWriter(f);

                // 缓存流必须建立在一个存在的流的基础上              

                PrintWriter pw = new PrintWriter(fw);              

        ) {

            pw.println("garen kill teemo");

            pw.println("teemo revive after 1 minutes");

            pw.println("teemo try to garen, but killed again");

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

有的时候,需要立即把数据写入到硬盘,而不是等缓存满了才写出去。 这时候就需要用到flush

public class TestStream {

    public static void main(String[] args) {

        //向文件lol2.txt中写入三行语句

        File f =new File("d:/lol2.txt");

        //创建文件字符流

        //缓存流必须建立在一个存在的流的基础上

        try(FileWriter fr = new FileWriter(f);PrintWriter pw = new PrintWriter(fr);) {

            pw.println("garen kill teemo");

            //强制把缓存中的数据写入硬盘,无论缓存是否已满

                pw.flush();           

            pw.println("teemo revive after 1 minutes");

                pw.flush();

            pw.println("teemo try to garen, but killed again");

                pw.flush();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

}

数据流:

DataInputStream 数据输入流 

DataOutputStream 数据输出流

使用数据流进行读写:

public class TestStream {

    public static void main(String[] args) {

        write();

        read();

    }

    private static void read() {

        File f =new File("d:/lol.txt");

        try (

                FileInputStream fis  = new FileInputStream(f);

                DataInputStream dis =new DataInputStream(fis);

        ){

            boolean b= dis.readBoolean();

            int i = dis.readInt();

            String str = dis.readUTF();

            System.out.println("读取到布尔值:"+b);

            System.out.println("读取到整数:"+i);

            System.out.println("读取到字符串:"+str);

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    private static void write() {

        File f =new File("d:/lol.txt");

        try (

                FileOutputStream fos  = new FileOutputStream(f);

                DataOutputStream dos =new DataOutputStream(fos);

        ){

            dos.writeBoolean(true);

            dos.writeInt(300);

            dos.writeUTF("123 this is gareen");

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

对象流:是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘,一个对象以流的形式进行传输,叫做序列化。 该对象所对应的类,必须是实现Serializable接口

学生类实现Serializable

public class Student implements Serializable{

        public String name;

        public int age;

}

public class TextFileObject {

    public static void main(String[] args) {

    //创建一个学生对象

    //要把Student对象直接保存在文件上,务必让Student类实现Serializable接口

    Student stu = new Student();

    stu.name="小王";

    stu.age=18;

    //准备一个文件用于保存该对象

    File f = new File("d:/hero.txt");

    //创建对象输出流

    try(FileOutputStream fos = new FileOutputStream(f);

    ObjectOutputStream oos = new ObjectOutputStream(fos);

    //创建对象输入流

    FileInputStream fis = new FileInputStream(f);

    ObjectInputStream ois = new ObjectInputStream(fis);){

    oos.writeObject(stu);

    Student s = (Student) ois.readObject();

    System.out.println(s.name);

    System.out.println(s.age);

    } catch (Exception e) {

    // TODO: handle exception

}

}

}

System.in

public class TestStream {

    public static void main(String[] args) {

        // 控制台输入

        try (InputStream is = System.in;) {

            while (true) {

                // 敲入a,然后敲回车可以看到

                // 97 13 10

                // 97是a的ASCII码

                // 13 10分别对应回车换行

                int i = is.read();

                System.out.println(i);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

使用Scanner读取

public class TestStream {

    public static void main(String[] args) {

            Scanner s = new Scanner(System.in);

            while(true){

                String line = s.nextLine();

                System.out.println(line);

            }

    }

}

使用Scanner从控制台读取整数

public class TestStream {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int a = s.nextInt();

        System.out.println("第一个整数:"+a);

        int b = s.nextInt();

        System.out.println("第二个整数:"+b);

    }

}

相关文章

  • Java之IO流详解

    title: Java之IO流详解tags: Java IO流categories: Java IO流 大多数应用...

  • Java基础之IO流

    ##Java基础之IO流IO流常用几个类的关系如下: 字节流 字节输入流FileInputStream 读取文件用...

  • java IO入门笔记

    1.java IO流的概念,分类,类图 1.1. java IO 流的概念 java的io是实现输入和输出的基础,...

  • Java IO详解

    1 Java IO流的概念,分类 1.1 Java IO流的概念 java的IO是实现输入和输出的基础,可以方便的...

  • Java基础之IO流

    Java基础之IO流 简单介绍Java中有两种IO流,第一种是以Steam结尾的字节流,第二种是以Writer或者...

  • java基础之IO流

    IO流上:概述、字符流、缓冲区(java基础) IO流结构图 FilterInputStream、FilterOu...

  • Java基础之IO流

    流就是当不同的介质之间有数据交互的时候,JAVA就使用流来实现。数据源可以是文件,还可以是数据库,网络甚至是其他的...

  • JAVA之IO流基础

    1.A:IO技术概述 * a: Output 把内存中的数据存储到持久化设备上这个动作称为输出(写)Out...

  • Java基础之IO流

    1.IO流 Java中有几种类型的流 答:字节流,字符流。字节流继承于InputStream、OutputStre...

  • Java基础之IO流

    什么是IO流?  IO是指应用程序对设备数据的输入输出操作。流的本质是数据传输。例如:键盘是输入设备,而显示器则是...

网友评论

      本文标题:Java基础之IO流

      本文链接:https://www.haomeiwen.com/subject/xjreiftx.html