I/O流----(字节流、字符流、缓冲流、对象流的上下转换)使用深入

IO流

1、IO流究竟能解决什么问题

IO流解决的是 数据从输入设备到内存   从内存到硬盘  从硬盘到硬盘之间 数据的传输问题
​
输入设备: 
   语音的输入
   键盘的输入
   鼠标的输入
输入设备到内存:就可以将我们通过输入设备输入的数据保存到内存上  内存:速度快  断电的话数据会丢失...
从内存到硬盘:可以将输入到内存的数据 持久化到硬盘上 就是保存到 硬盘上
从硬盘到硬盘:将硬盘上的一个文件保存到另一个硬盘上
​
I:in (入)
​
O:out(出)
​
包含了输入 和 输出

2、File的基本方法的使用

package com.qfedu.edu.file;
​
import java.io.File;

public class FileTest {
​
    public static void main(String[] args) {
​
        String basePath = "G:\\360MoveData\\Users\\apple\\Desktop\\test";
​
        // File.separator :文件分隔符
        String filePath = basePath + File.separator + "a.txt";
        System.out.println("打印出的路径是:" + filePath);
​
        String filePath1 = "G:\\360MoveData\\Users\\apple\\Desktop\\test/a.txt";
​
        File file = new File(filePath1);
​
        if(file.exists()){
            System.out.println("文件存在.....");
        }
​
    }
}
​
package com.qfedu.edu.file;
​
import java.io.File;
​
public class FileTest2 {
​
    public static void main(String[] args) {
        //地址分隔符的使用
        //  第一种分隔符的应用  原本是一个 \  但是在Java中他是一个转义字符  转意之后  \\
        // 第一种写法是windows下 文件分隔符的写法
        String path = "G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt";
        //这个就是第二种写法 /
        //第二种写法 是 Linux下文件分隔符的写法
        // 在windows下是兼容Linux下文件分隔符的写法的  所以两种的写法都可以
        String path1 = "G:/360MoveData/Users/apple/Desktop/test/a.txt";
​
        //现在可以测试下两种路径是否是正确的?
        // File类他的主要功能是找目标文件的  波波老师 啥是目标文件? 就是咋们要操作的文件就称为目标文件
        File file = new File(path);
        File file1 = new File(path1);
        //下面可以判断上面的文件是否存在/ 如果存在 说明分割师是没有问题的
        if (file.exists()) {
            System.out.println("第一个验证文件是存在的....");
        }
        if (file1.exists()) {
            System.out.println("第二个验证文件是存在的....");
        }
    }
}
​
package com.qfedu.edu.file;
​
import java.io.File;
​
​
public class FileTest3 {
​
    public static void main(String[] args) {
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\bobo");
        //创建文件夹
       /* if(!file.exists()){
             //创建文件夹(创建单个的文件夹 不能级联创建)
             file.mkdir();
        }*/
         if(!file.exists()){
             //创建文件夹(这个就能级联的创建文件夹)
             file.mkdirs();
        }
​
        //能不能删除文件夹呢?
       /* if(file.exists()){
             //这个只能删除单个的文件夹
             file.delete();
        }*/
        if(file.exists()){
             //这里要玩下级联删除
            deleteFolder(file);
        }
​
    }
​
    /**
     * 级联删除文件
     * @param folder
     */
    public static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteFolder(file);
                }
            }
        }
        folder.delete();
    }
}
​
package com.qfedu.edu.file;
​
import java.io.File;
import java.io.IOException;
​
public class FileTest4 {
​
    public static void main(String[] args) throws IOException {
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\bobo.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        //能不能删除文件呢?
        file.delete();
        //下面研究下File类中常见的方法
        File fileTemple = File.createTempFile("xiaobobo", ".txt");
        if(fileTemple.exists()){
            System.out.println("临时文件是存在的....");
        }
​
        if(fileTemple.isDirectory()){
            System.out.println("当前的路径是不是文件夹");
        }
​
        if(fileTemple.isFile()){
            System.out.println("判断是不是文件");
        }
    }
}
package com.qfedu.edu.file;
​
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
​
public class FileTest5 {
​
    public static void main(String[] args) throws IOException {
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test");
        //遍历当前路径下 所有的文件以及文件夹
        File[] files = file.listFiles();
        for (File f:files){
            System.out.println("名字是:"+f.getName());
        }
        System.out.println("----------------------");
​
        //当前路径下所有和文件夹的名字
        String[] list = file.list();
        for (String str:list){
            System.out.println("数据:"+str);
        }
​
        System.out.println("----------------------");
​
        //获取当前File的绝对路径
        //问题来了什么是绝对路径:带盘符的路径G:/xiaobobo/xiaowangzi/aa.txt
        //还有一种路径称为相对路径; 相当:可以是当前项目  也可以是后面项目的部署路径
​
        File file1 = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
        String absolutePath = file1.getAbsolutePath();
        System.out.println("file1的绝对路径是:"+absolutePath);
​
        //这个是获取的是他爹的目录
        File parentFile = file1.getParentFile();
        System.out.println("爹的目录是:"+parentFile.getAbsolutePath());
        System.out.println("当前文件或者文件夹的名字:"+file1.getName());
    }
}

3、IO流的分类

字节流、字符流、对象流、转换流、缓冲流

4、字节流的使用

4.1、字节的输入流
字节流读取的是字节
​
字节流是可以读写任意文件的
​
i:输入(读进来的数据)
​
    InputStream  :这个就是字节流中输入流的爹
​
o:输出(写出去的数据)
    
    OutputStream:字节流中输出流的爹 
package com.qfedu.edu.inputstream;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
​
​
public class Test001 {
​
    public static void main(String[] args){
        FileInputStream in=null;
        try {
            //第一个步骤:找到目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
            //第二步:建立输入输出通道
            in = new FileInputStream(file);
            //第三步:读写流的数据
            //  in.read()   //没有参数这个代表的是一个字符一个字符的去读  如果没有字符了 那么返回 -1
            //现在需要使用 in.read()将所有的内容全部读取出来?
​
            int unicodeVal=0;
            while ((unicodeVal=in.read())!=-1){
                System.out.print((char) unicodeVal);
            }
​
        }catch (IOException err){
            System.out.println("读取文件出现了异常....");
        }finally {
            //第四步:关闭流
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("关闭流的时候出现了问题");
                throw new RuntimeException(e);
            }
        }
​
​
    }
​
}
​
package com.qfedu.edu.inputstream;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
​

public class Test002 {
​
    public static void main(String[] args){
        FileInputStream in=null;
        try {
            //第一个步骤:找到目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
            //第二步:建立输入输出通道
            in = new FileInputStream(file);
            //第三步:读写流的数据
            //  in.read()   //没有参数这个代表的是一个字符一个字符的去读  如果没有字符了 那么返回 -1
            int read = in.read();  //这个读取到的是 unicode这个码值
            char char1= (char) read;
            System.out.println("读取到的内容是:"+char1);
        }catch (IOException err){
            System.out.println("读取文件出现了异常....");
        }finally {
            //第四步:关闭流
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("关闭流的时候出现了问题");
                throw new RuntimeException(e);
            }
        }
​
​
    }
​
}
​
package com.qfedu.edu.inputstream;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
​
public class Test003 {
​
    public static void main(String[] args){
        FileInputStream in=null;
        try {
            //第一个步骤:找到目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
            //第二步:建立输入输出通道
            in = new FileInputStream(file);
            //in.read(byte[])  这个表示的是将数据读取到缓冲区  那这个一次读取的就不是一个字节了  而是缓冲区大小个 如果是文件的结尾的话 那么勘定读取到的数据 是小于等于这个缓冲区大小的
            byte[] buf=new byte[1024];
            //下面将内容直接读取到 缓冲区
            int length= in.read(buf);   //这里的返回值 就不是读取的内容了 而是这一次读取字节的个数(长度)
            //接下来我们就可以将这个内容直接打印出来了....
            //byte[]数组怎么整成String
           // String val = new String(buf, "UTF-8");
            String val1 = new String(buf,0,length,"UTF-8");
            System.out.println("读取到的内容是:"+val1);
        }catch (IOException err){
            System.out.println("读取文件出现了异常....");
        }finally {
            //第四步:关闭流
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("关闭流的时候出现了问题");
                throw new RuntimeException(e);
            }
        }
​
​
    }
​
}
​
package com.qfedu.edu.inputstream;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
​
​
public class Test004 {
​
    public static void main(String[] args){
        FileInputStream in=null;
        try {
            //第一个步骤:找到目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
            //第二步:建立输入输出通道
            in = new FileInputStream(file);
            //in.read(byte[])  这个表示的是将数据读取到缓冲区  那这个一次读取的就不是一个字节了  而是缓冲区大小个 如果是文件的结尾的话 那么勘定读取到的数据 是小于等于这个缓冲区大小的
            byte[] buf=new byte[7];
            //下面将内容直接读取到 缓冲区
            //显然下面的方法就一次性不能读取完成....
            //int length= in.read(buf);   //这里的返回值 就不是读取的内容了 而是这一次读取
​
            //那就多读取几次就能读取完成....
            int length=0;
            //申明一个字符串来进行字符的拼接
            StringBuilder stringBuilder=new StringBuilder();
            while ((length=in.read(buf))!=-1){
                String val = new String(buf,0,length, "UTF-8");
                stringBuilder.append(val);
            }
​
            System.out.println("结果是:"+stringBuilder.toString());
        }catch (IOException err){
            System.out.println("读取文件出现了异常....");
        }finally {
            //第四步:关闭流
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("关闭流的时候出现了问题");
                throw new RuntimeException(e);
            }
        }
​
​
    }
​
}
​
package com.qfedu.edu.inputstream;
​
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
​
​
public class Test005 {
​
    public static void main(String[] args){
        FileInputStream in=null;
        try {
            //第一个步骤:找到目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
            //第二步:建立输入输出通道
            in = new FileInputStream(file);
​
            byte[] buf=new byte[10];
​
            int readLength = in.read(buf, 0, 7);
​
            System.out.println("结果是:"+new String(buf,0,readLength));
        }catch (IOException err){
            System.out.println("读取文件出现了异常....");
        }finally {
            //第四步:关闭流
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("关闭流的时候出现了问题");
                throw new RuntimeException(e);
            }
        }
    }
}
​
4.2、字节输出流
-----OutputStream
---------FileOutputStream  文件的字节输出流
4.2.1、字节流写出内容的第一种方式
package com.qfedu.edu.outputstream;
​
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class Test001 {
​
    public static void main(String[] args) {
        FileOutputStream out = null;
        try {
            //找目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\xiaobobo.txt");
            //第二步:建立输入和输出通道
            out = new FileOutputStream(file);
            //第三步:读写流的内容
            //  JNI (Java  Native Interface)
            // 这里写int的时候 写的不是真正的int 而是认为这个int是一个unicode值 写出内容的是要进行转码
            out.write(67);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
​
    }
}
​
4.2.2、写byte[]类型的数组内容出去
package com.qfedu.edu.outputstream;
​
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
​
public class Test002 {
​
    public static void main(String[] args) {
        FileOutputStream out = null;
        try {
            //找目标文件
            File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\xiaobobo.txt");
            //第二步:建立输入和输出通道
            out = new FileOutputStream(file);
            //第三步:读写流
            out.write("我是二狗子".getBytes("UTF-8"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
4.2.3、字节流和字符流实现文件的复制
package com.qfedu.edu.inputandoutput;
​
import java.io.*;
​
public class Test001 {
​
    public static void main(String[] args) {
        //第一步:找到目标文件
        File fileIn = new File("G:\\meinv.jpg");
        File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\image\\meinv.jpg");
        //第二步:建立输入和输出通道
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(fileIn);
            out = new FileOutputStream(fileOut);
            //第三步:读写流
            //一次性不能读完....多读几次
            //申明一个读的缓冲区
            byte[] buf = new byte[100];
            int readLength = 0;
            while ((readLength = in.read(buf)) != -1) {
                //读一次的内容 那么就写一次
                out.write(buf,0,readLength);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
                out.flush();
                out.close();
            } catch (Exception err) {
                System.out.println("关闭流出现问题....");
            }
        }
    }
}

5、字符流的使用

字节流和字符流之间的关系是啥?
​
  字节流是不能读取汉字的 字符流使能读取汉字的....
  
  他们之间的关系是啥呢? 读取的时候 都读取的是 字节  但是字符流=字节流+转码器  这个转码器就能读取汉字
  
虽然我们的案例中采用了字节流来读取汉字 能正常显示  并不是因为字节流能读取汉字 而是因为 String这个类中自带
​
转码器  
​
字符流因为自带转码器 所以他的应用场景是处理字符文件
​
一般情况下 字符流如果是处理  图片 视频之内的 是无法处理的 ... 这个时候 只能采用字节流来处理
5.1、文件字符流读的使用
--------------Reader
------------------FileReader
5.1.1、读取内容
package com.qfedu.edu.reader;
​
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
​
​
public class Test001 {
​
    public static void main(String[] args) {
        //第一步:找到目标文件
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
        FileReader fileReader = null;
        try {
            //建立输入输出通道
            fileReader = new FileReader(file);
            //读写流的内容
            //一个一个的读
            int read = fileReader.read();
            System.out.println("内容是:" + (char) read);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
package com.qfedu.edu.reader;
​
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
​
​
public class Test002 {
​
    public static void main(String[] args) {
        //第一步:找到目标文件
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
        FileReader fileReader = null;
        try {
            //建立输入输出通道
            fileReader = new FileReader(file);
            //读写流的内容
            int val = fileReader.read();
            while (val!=-1){
                System.out.println("内容是:"+(char)val);
                val=fileReader.read();
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
​
​
    }
}
​
5.1.2、读汉字
package com.qfedu.edu.reader;
​
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
​
​
public class Test003 {
    public static void main(String[] args) {
        //第一步:找到目标文件
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
        FileReader fileReader = null;
        try {
            //建立输入输出通道
            fileReader = new FileReader(file);
            //读写流的内容
            char[] buf=new char[8192];
            int readLength=fileReader.read(buf);
            //我要看中文
            String val = new String(buf, 0, readLength);
            System.out.println("内容是:"+val);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
5.2、文件字符输出流
---------Writer
-------------FileWriter  默认缓冲区的大小是 1024
​
所有的字符输出流自带缓冲区
5.2.1、FileWriter的基本使用
public class Test001 {
​
   public static void main(String[] args){
​
      //找到目标文件(内容输出位置)
      File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\a.txt");
      FileWriter fileWriter=null;
      try {
         //建立输入输出通道
         fileWriter = new FileWriter(fileOut);
         //读写流
         //写int的时候还是写的是 unicode这个码值
         //fileWriter.write(65);
         //还能直接写字符串
         //fileWriter.write("我是中国人");
​
        // fileWriter.write("中国好".toCharArray());
         //写一个字符串的一部分
         //fileWriter.write("abcdefg",0,3);
      } catch (IOException e) {
         throw new RuntimeException(e);
      }finally {
         if(null!=fileWriter){
            try {
               fileWriter.flush();
               fileWriter.close();
            }catch (Exception err){
               System.out.println("关闭流出现问题....");
            }
         }
      }
   }
}
5.3、字符流的综合案例
public static void main(String[] args) throws Exception {
        //找到目标文件
        File fileIn = new File("G:\\meinv.jpg");
        File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\image\\meinv.jpg");
        //建立输入输出通道
        FileReader fileReader = new FileReader(fileIn);
        FileWriter fileWriter = new FileWriter(fileOut);
        //读写流
        char[] buf = new char[8192];
        int readLength = 0;
        while ((readLength = fileReader.read(buf)) != -1) {
            fileWriter.write(buf, 0, readLength);
        }
        fileReader.close();
        fileWriter.close();
    }

6、对象流的使用

所谓的对象流:简单的说可以直接将Java对象写入到文件 这个过程其实 就是序列化的过程
​
还可以将文件中的内容直接转换成Java对象  这个过程实际上就是咋们的 反序列化
6.1、对象输出流
6.1.1、单个对象的输出流
 public static void main(String[] args) throws Exception {
        //第一步:也要找目标文件
        File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\object.txt");
        //第二步:建立输入输出通道
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileOut));
        //第三步:读写流的数据
        //准备数据
        User user = new User(1, "xiaobobo", "123");
        out.writeObject(user);
        //第四步:就可以完成这个流的关闭
        out.flush();
        out.close();
    }
6.1.2、集合对象的输出流
public static void main(String[] args) throws Exception {
        //第一步:也要找目标文件
        File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\object.txt");
        //第二步:建立输入输出通道
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileOut));
        //第三步:读写流的数据
​
        List<User> userList=new ArrayList<>();
        userList.add(new User(1, "xiaobobo", "123"));
        userList.add(new User(2, "xiaobobo", "123"));
        userList.add(new User(3, "xiaobobo", "123"));
        //准备数据
        out.writeObject(userList);
        //第四步:就可以完成这个流的关闭
        out.flush();
        out.close();
    }
6.1.3、单个对象的输入流的测试
 public static void main(String[] args) throws Exception {
        //第一步:也要找目标文件
        File fileIn = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\object.txt");
        //第二步:建立输入输出通道
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileIn));
        //第三步:读写流的数据
        User user = (User) in.readObject();
        System.out.println("从文件中读取到的这个流的对象是:"+user);
        in.close();
    }
6.1.4、集合对象的输入流的测试
    public static void main(String[] args) throws Exception {
        //第一步:也要找目标文件
        File fileIn = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\object.txt");
        //第二步:建立输入输出通道
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileIn));
        //第三步:读写流的数据
        ArrayList<User> user = (ArrayList<User>) in.readObject();
        System.out.println("从文件中读取到的这个流的对象是:"+user);
        in.close();
    }

7、转换流的使用

转换流简单的说 就是可以将字节流转换成字符流
​
------------InputStreamReader   :将字节流的读 转换成字符流的读  这个读配置文件效率高
​
------------OutputStreamWriter  :这个就表示的是将字节流的写 转换成字符流的写
​
编码的由来:
   编码:相当于是有个表格  这个表格中的字符都对用了一个码值
   最先出现的是 ASCI编码  使用了一个字节来表示  256中情况  字母  标点....   这个是美国人搞的 
   后来欧洲人觉得 美国人都搞了个编码 我们也要搞一个编码 ISO-8859-1 在美国人的ASCI基础上进行拓展 郎阔了欧洲所有国家的语言和文字
   这个时候就存在一个人问题了...
   特是用两个字节来表示的   2个字节 16位  一共可以表示 65536中情况  这个时候就出现了一种新的情况 码表中存在大量的空白 就只有码值 但是没有对应的字符 
   
   再后来中国人搞了个 GB2312  这个时候中国人也采用的是 2字节 65536中情况来 编码汉字以及 郎阔了欧洲人的这个编码 以及 ASCI的这个编码  但是还是存在 大量的空白的问题
   
   于是在我们开发的时候如果是采用一种编码来编码 采用另一种编码来解码的话 就会出现可能读取到的位置 只有码值没有字符的情况  于是JVM也不知道 该显示什么 于是就显示 ???
7.1、字节输出流转换成字符输出流
 public static void main(String[] args) throws IOException {
        File file = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\test.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter out = new OutputStreamWriter(fileOutputStream);
        out.write("我是中国人民的儿子我热爱我的祖国和人民");
        out.close();
  }
7.2、字节输入流转换成字符输入流
 public static void main(String[] args) throws IOException {
        InputStream in = Test001.class.getClassLoader().getResourceAsStream("db.properties");
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        char[] buf=new char[8192];
        int readlength = inputStreamReader.read(buf);
        //接下来再将这个内用给打印出来
        String val = new String(buf, 0, readlength);
        System.out.println("这个就是咋们的内容:"+val);
        inputStreamReader.close();
  }
 public static void main(String[] args) throws IOException {
        InputStream in=new FileInputStream(new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\object.txt"));
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        char[] buf=new char[8192];
        int readlength = inputStreamReader.read(buf);
        //接下来再将这个内用给打印出来
        String val = new String(buf, 0, readlength);
        System.out.println("这个就是咋们的内容:"+val);
        inputStreamReader.close();
 }

8、缓冲流的使用

所有这个流的前面只要是有Buffred开头的都是缓冲流
8.1、字节缓冲输入流
字符缓冲输入流 这个缓冲区的默认大小是 8192   最大的缓冲区大小是 Intege.MAX_VALUE-8
    public static void main(String[] args) throws IOException {
​
        File fileIn = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\test.txt");
        FileInputStream fileInputStream = new FileInputStream(fileIn);
        BufferedInputStream in = new BufferedInputStream(fileInputStream);
        //读写流
        int read = in.read();
        System.out.println("数据是:" + (char) read);
    }
8.2、字节缓冲输出流
public static void main(String[] args) throws IOException {
​
        //找目标文件
        File fileOut = new File("G:\\360MoveData\\Users\\apple\\Desktop\\test\\test.txt");
        //建立输入输出通道
​
        FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
        BufferedOutputStream out = new BufferedOutputStream(fileOutputStream);
​
        //读写流
        out.write("我是小波波".getBytes("UTF-8"));
​
        //关闭资源
        out.flush();
        out.close();
    }
8.3、字符缓冲输入流:

记住装饰器

字符流 = 字节流+转码器。

8.4、字符缓冲输出流:

  • 18
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值