IO流笔记

 

 一、编码,解码,文字,字节,字符数组

        /*
        编码,把文字转换成字节
         */
        String name = "abc我爱你中国";
        //将字符集存储到字符数组中
//        byte[] bytes = name.getBytes(); //以当前代码默认字符集进行编码(UTF-8)
        byte[] bytes = name.getBytes("GBK"); //指定编码
        System.out.println(bytes.length);
        //输出字符数组
        System.out.println(Arrays.toString(bytes));
        /*
        解码,把字节转换成对应的中文形式,编码和解码的字符集必须一致,否则乱码
         */
//        String rs = new String(bytes); //默认UTF-8
        String rs = new String(bytes,"GBK");
        System.out.println(rs);

/**
    目标:字节输入流的使用。

    IO流的体系:
                 字节流                                   字符流
        字节输入流            字节输出流               字符输入流        字符输出流
        InputStream          OutputStream           Reader           Writer  (抽象类)
        FileInputStream      FileOutputStream       FileReader       FileWriter(实现类,可以使用的)

    文件字节输入流:FileInputStream
        -- 作用:以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去。
                按照字节读文件数据到内存中。
        -- 构造器:
            public FileInputStream(File file):创建字节输入流管道与源文件对象接通
            public FileInputStream(String pathname):创建字节输入流管道与源文件路径接通。
        -- 方法:
            public int read(): 每次读取一个字节返回,读取完毕返回-1。

    小结:
        一个一个字节读取中文数据输出其实是被淘汰的,性能极差!
         一个一个字节读取中文数据输出,会出现截断中文字节的情况,无法避免读取中文输出乱码的问题。

 */

    public static void main(String[] args) throws IOException {
//        InputStream is = new FileInputStream(new File("src\\data.txt"));
        InputStream is = new FileInputStream("src\\data.txt");
//        //读取一个字节返回,每次读一滴水
//        int b1 = is.read();
//        System.out.println((char)b1);
//        int b2 = is.read();
//        System.out.println((char)b2);
//        int b3 = is.read();
//        System.out.println((char)b3);
//        int b4 = is.read();
//        System.out.println(b4); //读取完毕,返回-1
        //使用循环改进
        int b;
        while((b = is.read()) != -1){
            System.out.print((char)b);
        }
    }

    public static void main(String[] args) throws Exception {
        //创建一个文件字节输入流管道与源文件接通
        InputStream is = new FileInputStream("src\\data2.txt");
        //定义一个字节数组,用于读取字节数组
//        byte[] buffer = new byte[3];
//        int len = is.read(buffer);
//        System.out.println("读取了几个字节:" + len);
//        String rs = new String(buffer);
//        System.out.println(rs);
//
//        int len1 = is.read(buffer);
//        System.out.println("读取了几个字节:" + len1);
//        String rs1 = new String(buffer);
//        System.out.println(rs1);
//
//        int len2 = is.read(buffer);
//        System.out.println("读取了几个字节:" + len2);
//        //读取多少倒出多少
//        String rs2 = new String(buffer, 0, len2);
//        System.out.println(rs2);

        //使用循环改进,每次读取一个字节数组
        byte[] buffer = new byte[3];
        int len;
        while((len = is.read(buffer)) != -1){
            System.out.println(new String(buffer, 0, len));
        }
    }

/*
使用文件字节输入流一次读完全部字节,可以解决乱码问题
 */
public class FileInputStreamDemo03 {
    public static void main(String[] args) throws Exception {
        //创建一个字节输入流管道与源文件接通
        File f = new File("src\\data3.txt");
        InputStream is = new FileInputStream(f);
        //定义一个字节数组和文件大小一样大
        byte[] buffer = new byte[(int)f.length()];
        int len = is.read(buffer);
        System.out.println("读取了多少个字节:" + len);
        System.out.println("文件大小:" + f.length());
        System.out.println(new String(buffer));
//        byte[] buffer = is.readAllBytes(); //jdk9才出现的用法
//        System.out.println(new String(buffer));
    }
}
注:byte[] buffer = is.readAllBytes();(jdk9才出现的用法)

 

public class OutputStreamDemo04 {
    public static void main(String[] args) throws Exception {
        OutputStream os = new FileOutputStream("src\\out04.txt", true); //追加数据
//        OutputStream os = new FileOutputStream("src\\out04.txt"); //先清空文件中的数据,再写数据
        //写数据出去
        os.write('a');
        os.write(98);
        os.write("\r\n".getBytes());
        os.write('徐');
        //写一个字节数组出去
        byte[] buffer = {'a', 97, 98, 99};
        os.write(buffer);

        byte[] buffer2 = "我是中国人".getBytes();
        os.write(buffer2);
        os.write("\r\n".getBytes()); //换行

        byte[] buffer3 = {'a', 97, 98, 99};
        os.write(buffer3, 0, 3);
        os.write("\r\n".getBytes());
        //写数据必须刷新数据
//        os.flush();
        os.close(); //释放资源,包含了刷新
    }
}

/*
使用字节流完成文件的复制(支持一切文件的复制)
 */
public class CopyDemo05 {
    public static void main(String[] args) {
        try{
            //定义一个输入流管道与源文件接通
            InputStream is = new FileInputStream("src\\out04.txt");
            //定义一个输出流管道与目标文件接通
            OutputStream os = new FileOutputStream("src\\out05.txt");
            //定义一个字节数组用来转移数据
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            System.out.println("复制完成了");
            os.close();
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

改进代码:

public class TryCatchFinallyDemo01 {
    public static void main(String[] args) {
        InputStream is = null;
        OutputStream os = null;
        try{
            //定义一个输入流管道与源文件接通
            is = new FileInputStream("src\\out04.txt");
            //定义一个输出流管道与目标文件接通
            os = new FileOutputStream("src\\out05.txt");
            //定义一个字节数组用来转移数据
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            System.out.println("复制完成了");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //无论代码正常还是出现异常,都会执行这里
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符流

/**
 目标:字符输入流的使用。

 IO流的体系:
 字节流                                   字符流
 字节输入流           字节输出流               字符输入流       字符输出流
 InputStream         OutputStream            Reader         Writer     (抽象类)
 FileInputStream     FileOutputStream        FileReader     FileWriter (实现类)

 c.FileReader:文件字符输入流。
 -- 作用:以内存为基准,把磁盘文件的数据以字符的形式读入到内存。
 简单来说,读取文本文件内容到内存中去。

 -- 构造器:
 public FileReader(File file):创建一个字符输入流与源文件对象接通。
 public FileReader(String filePath):创建一个字符输入流与源文件路径接通。

 -- 方法:
 public int read(): 读取一个字符的编号返回! 读取完毕返回-1
 public int read(char[] buffer):读取一个字符数组,读取多少个字符就返回多少个数量,读取完毕返回-1
 小结:
 字符流一个一个字符的读取文本内容输出,可以解决中文读取输出乱码的问题。
 字符流很适合操作文本文件内容。
 但是:一个一个字符的读取文本内容性能较差!!
 */
public class FileReaderDemo01 {
    public static void main(String[] args) throws Exception {
        Reader fr = new FileReader("src\\data3.txt");
        //读取一个字符返回,没有可读取的字符返回-1
//        int code = fr.read();
//        System.out.println((char)code);
        //使用循环读取字符
        int code;
        while((code = fr.read()) != -1){
            System.out.print((char)code);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值