Java之字节流和字符流

一.字节流

流分为输入流和输出流。
用参照物(程序)来判断是输出流还是输入流。
注意:在用流的时候,结束时要记得关掉流。

字节流 将文字、图片、音频等转成字节,进行数据传输。

  • 程序——>文件 输出流 写文件
  • 文件——>程序 输入流 读文件
  • 以下所有字节流的父类, 是抽象类
    • OutputStream: 输出流
    • InputStream : 输入流

字节流输出流(写入文件的方法)代码:

public class Kll {
    public static void main(String[] args) {
        // 创建一个字节输出流 写文件
        // 创建流 绑定写入文件路径
        // 写入时,[路径正确情况下]系统会帮你创建出文件(没有此文件的情况下),否则会覆盖原文件
        File file = new File("/Users/lanou/Desktop/Test/kll.txt");
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/kll1.txt");
        // 写入方法
        // 该方法是按ASCII码写入文件
        fos.write(48);
        fos.write(49);
        fos.write(66);

        // 利用字节数组输入
        byte[] b = {66, 67, 68, 69};
        fos.write(b);
        // "konglinglei"转成字节数组 getBytes()
        fos.write("konglinglei".getBytes());

        // 按偏移量写入数组字符
        fos.write(b, 1, 2);
        // 关闭资源
        fos.close();
    }
}

字节流输入流(读取文件的方法)代码:
1.一个一个读取

public class Kll {
    public static void main(String[] args) {
        // 读取
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/kll1.txt");
        // 一个一个字节读取,按照ASCII码读取
        int read = fis.read();
        System.out.println((char)read);
         read = fis.read();
        System.out.println((char)read);
         read = fis.read();
        System.out.println((char)read);
        // 文件读取完毕会返回-1
         read = fis.read();
        System.out.println(read);
        fis.close();

        // 循环快捷读取方法
        /*
        int num = 0;
        while ((num = fis.read()) != -1) {
            System.out.println((char)num);
        }
        fis.close();
        */
    }
}

2.数组读取

public class Kll {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/kll1.txt");
        // 利用字节数组读
        // 实际上把读取的内容放回数组中了
        // read(byte[] b) 方法返回值是读取到的有效数字,读完之后,返回的也是-1.
        // byte数组长度,一般填1024或者1024整数倍
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = fis.read(b)) != -1) {
            // 使用字符长的构造方法打印  String(byte[] b, offset,length)
            String string = new String(b, 0, len);
            System.out.println(string);
            System.out.println(len);
        }
        fis.close();
    }
}

二.字符数组流

与字节流方法大同小异。

  • 字符流(读写文档)
  • 所有字符流的父类, 是抽象类
    • Writer
    • Reader
  • FileWriter
  • FileReader
  • 注意:在未关闭资源时,需要刷新才能把写入的文本放入文本中。具体看下列代码解释。

1.字符输出流代码:

public class Kll {
    public static void main(String[] args) {
        // 创建字符输出流
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test11/wl.txt");
        // 写入文件
        fw.write(65);
        // 刷新 之后,会将写入的内容写入在文本中.
        //fw.flush();
        // 字符数组写入
        char[] c = {'了', '5', '8', '9'};
        fw.write(c);
        // 字符串直接写入
        // Mac 中 换行符 \n win /r /n
        fw.write("\n离离原上草\n一岁一枯荣\n野火烧不尽\n春风吹又生");

        // 资源关闭之前,系统会帮你刷新一下
        fw.close();
    }
}

2.字符输入流:

public class Kll {
    public static void main(String[] args) {
    // 创建字符输入流
        FileReader fr = new FileReader("/Users/lanou/Desktop/Test11/wl.txt");
        // 一个字节一个字节读
        /*int read = 0;
        while ((read = fr.read()) != -1) {
            System.out.print((char)read);
        }
        fr.close();
        */
        // 用数组读取
        char[] cbuf = new char[1024];
        int num = 0;
        while ((num = fr.read(cbuf)) != -1) {
            String string = new String(cbuf, 0, num);
            System.out.println(string);
            System.out.println(num);
        }
        fr.close();
    }
}

猜你喜欢

转载自blog.csdn.net/KongLingLei_08225/article/details/82693152