字节流和字符流的用法

字节流

作用:字节流可以将文字图片音频等等文件转成字节进行数据传输
分为:
OutputStream: 输出流,写文件
InputStream: 输入流,读文件

输出流的使用实例:


        //  创建一个字节输出流,写文件,设置文件路径,如果没有,系统会自动创建
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/dd.txt");
        //  写入方法write
        //  该方法是按ASCII码写入的
        fos.write(65);
        //  利用字节数组写入,同样是按ASCII码写入
        byte[] b = {66,67,68,69};
        fos.write(b);
        //  将"heihei" 转成字节数组写
        fos.write("heihei".getBytes());
        //  按偏移量写入数组的字符,b代表数组名,1代表数组角标,2代表长度
        fos.write(b, 1, 2);
        //  关闭资源 
        fos.close();

写入文件的是:A B C D E h e i h e i e i

注意:写完要关闭资源,一般会判断一下,创建流时有可能发生异常,利用finally特性补充一下判断

finally {
            if (fos != null) {          
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException("关闭失败");
                }
            }
        }

输入流用来读取文件,有三种方法

  • 单个读取
        //  设置读取路径
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/dd.txt");
        //  fis.read读取
        int num = fis.read();
        //  转为字符输出,不然输出的ASCII的对应值
        System.out.println((char)num);
        //  继续读取
        num = fis.read();
        System.out.println((char)num);
        //  关闭资源
        fis.close();

注意:当文件读取完毕时,返回值为-1

  • 循环读取
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/dd.txt");
        int num = 0;
        //  fis.read()为-1时,文件读取完毕
        while ((num = fis.read()) != -1) {
            System.out.println((char)num);
            }
            fis.close();
  • 利用字节数组读取

        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/dd.txt");
        //  创建数组,数组长度一般为1024的倍数
        byte[] b = new byte[1024];
        //  接收有效长度
        int len = 0;
        //  fis.read(b)返回值是有效长度     
        while ((len = fis.read(b)) != -1) {
            //  使用字符串的构造方法打印
            System.out.println(new String(b, 0, len));
        }
        fis.close();    

字符流

分为:
FileWriter: 写入文件
FileReader: 读取文件

输出流使用方法

        //  创建字符输出流
        FileWriter fw = new FileWriter("/Users/lanou/Desktop/hh.txt");
        //  写入文件
        fw.write(65);
        //  刷新(会将内容写入到文件中,如果不刷新,将不会写入)
        fw.flush();     
        //  字符数组写入
        char[] c ={'7','8'};
        fw.write(c);
        fw.flush();
        //  字符串直接写入
        //  换行 \n (mac系统)
        fw.write("天下三分明月夜\n");
        fw.write("二分无赖是扬州\n");
        fw.flush();     
        //  关闭资源
        //  关闭前,系统自动刷新
        fw.close();

写入文件的是:
A78
天下三分明月夜
二分无赖是扬州

输入流读取文件使用,同样三种方法(与字节流基本相同)

  • 单个读取
        //  设置读取路径
        FileReader fr = new FileReader("/Users/lanou/Desktop/dd.txt");
        int num = fr.read();
        System.out.println((char)num);
        //  继续读取
        num = fr.read();
        //  强转为字符输出
        System.out.println((char)num);
        //  关闭资源
        fr.close();
  • 循环读取

        //  设置读取路径
        FileReader fr = new FileReader("/Users/lanou/Desktop/dd.txt");
        int num = 0;
        while ((num = fr.read()) != -1) {
        //  强转为字符输出
            System.out.print((char)num);
        }
        fr.close(); 
  • 利用字符数组读取
        FileReader fr = new FileReader("/Users/lanou/Desktop/dd.txt");
        char[] c = new char[1024];
        int len = 0;
        while ((len = fr.read(c)) ! = -1) {
            System.out.println(new String(c, 0, len));  
        }
        fr.close();

猜你喜欢

转载自blog.csdn.net/guxin0729/article/details/82694849