io 流简述:

i->inputStream(输入流) o->outputStream  (输出流)

IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间的数据传输,Java对于数据的操作都是通过流实现,而java用于操作流的对象都在IO包中。

java 中的流是一个数据流,即可以在流中读取数据,也可以在流中写入数据。

source ——>inputStream/Reader——>program

program——>outputStream/writer——>target

inputStream/Reader 是与数据源相关,outputStream/writer与接收数据的目标相关。

IO流常用的基类:

* InputStream , OutputStream

字符流的抽象基类:

* Reader , Writer

由上面四个类派生的子类名称都是以其父类名作为子类的后缀:

如:FileReader和FileInputStream    FileWriter和FileOutputStream

File类是IO包中唯一代表磁盘文件本身的对象。通过File来创建,删除,重命名文件。File类对象的主要作用就是用来获取文本本身的一些信息。如文本的所在的目录,文件的长度,读写权限等等。(有的需要记忆,比如isFile(),isDirectory(),exits();有的了解即可。使用的时候查看API)

字符流简介:

* 字符流中的对象融合了系统默认的编码表,一般都是GBK编码,字符流只用来处理文本数据,而字节流用来处理媒体数据。数据最常见的表现形式是文件,字符流用于操作文件的子类,一般是FileWriter和FileReader.

读写:

* 字符流的每次操作后后需要flush()刷新,用完需要关闭close()流,使用对象抛出IO异常

实例:

public static void main(String[] args) {

        String filePath = "s://io.txt";
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter write = null;
        try {
            write = new FileWriter(file);
            write.write("阿萨德");//写入数据
            write.flush(); // 刷新
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(write!=null){
                try {
                    write.close(); //关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        

    }

  

public static void main(String[] args) {

        String filePath = "s://io.txt";
        File file = new File(filePath);
        FileReader read = null;
        try {
            if (!file.exists()) {
                throw new Exception("文件不存在");
            }
            read = new FileReader(file);
            char[] buf = new char[100];
            int i;
            while ((i = read.read(buf)) != -1) {
                read.read(buf, 0, i);
            }
            System.out.println(new String(buf));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
}

缓冲区:

 字符流的缓冲区:BufferedReader和BufferedWreiter

缓冲区是为了提高流读写效率而出现的

读取流缓冲区提供了一个一次读一行的方法readline,方便对文本数据的获取。
readline()只返回回车符前面的字符,不返回回车符。如果是复制的话,必须加入newLine(),
写入回车符newLine()是java提供的多平台换行符写入方法。

 使用缓冲区读取操作:

String filePath = "s://io.txt";
        File file = new File(filePath);
        FileReader read = null;
// 写入 FileWrite write = null; try { if (!file.exists()) { throw new Exception("文件不存在"); } read = new FileReader(file); BufferedReader br = new BufferedReader(read);
String readLine = br.readLine(); System.out.println(readLine); } catch (Exception e) { e.printStackTrace(); } finally { if (read != null) { try { read.close(); } catch (IOException e) { e.printStackTrace(); } } }

 操作写入:

String filePath = "s://io.txt";
        File file = new File(filePath);
        FileWriter write = null;
        try {
            if (!file.exists()) {
                throw new Exception("文件不存在");
            }
            write = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(write);
            bw.write("万恶硬功夫业务俄国防");
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (write != null) {
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

 

字节流:

1、字节流和字符流的基本操作是相同的,但是要想操作媒体流就需要用到字节流。

2、字节流因为操作的是字节,所以可以用来操作媒体文件。(媒体文件也是以字节存储的)

3、读写字节流:InputStream   输入流(读)和OutputStream  输出流(写)

4、字节流操作可以不用刷新流操作。

5、InputStream特有方法:  int available();//返回文件中的字节个数

 简单实例 图片复制:

        String filePath = "s://Desert.jpg";
        String filePath2 = "s://test.jpg";
        File file = new File(filePath);
        File file2 = new File(filePath2);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file);
            if (!file2.exists()) {
                file2.createNewFile();
            }
            byte[] buf = new byte[1024];
            fos = new FileOutputStream(file2);
            int i;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }