Java基础程序——字节流和字符流

目录

一、IO流的分类

1.1、IO

1.2、IO流的分类

1.3、IO流的常用父类

1.4、IO程序书写

二、字节流

2.1、字节输出流 OutputStream

 2.2、字节输入流 InputStream

2.3、字节输入流InputStream

2.4、字节流练习

三、字符流

3.1、字符编码表

3.2、字符输入流Reader

3.3、字符输出流Write

3.4、 字符流复制文件


一、IO流的分类

1.1、IO

  • 即输入(Input)、输出(Output)。都是以Java程序为参照
  • 输入:把持久设备上的数据读取到内存中。
  • 输出:就内存中的数据存储到持久化设备上。

1.2、IO流的分类

  • java用于操作流的类都在IO包中
  • 流按流向分为两种:输入流、输出流
  • 流按操作类型分为两种:字节流、字符流
  1. 字节流:可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
  2. 字符流:只能操作村字符数据

1.3、IO流的常用父类

  • 字节流的抽象父类: InputStream, OutputSteam
  • 字符流的抽象父类:Reader, Writer

1.4、IO程序书写

  • 使用前:导入IO包中的类
  • 使用时:进行IO异常处理
  • 使用后:释放资源

二、字节流

2.1、字节输出流 OutputStream

方法:

  • void close() :关闭此输出流并释放与此流有关的所有系统资源。
  •  void write(byte[] b) :将 `b.length` 个字节从指定的 byte 数组写入此输出流。
  • void write(byte[] b, int off, int len) :将指定 byte 数组中从偏移量 `off` 开始的 `len` 个字节写入此输出流。
  • abstract  void write(int b) :将指定的字节写入此输出流。

作用:写入数据到文件

public class FileOutputStreamDeam {
    public static void main(String[] args) {
        File file = new File("D:\\实训\\Demo\\src\\test\\abc.txt");
        try{
            FileOutputStream fos = new FileOutputStream(file,true);
            byte[] data = "\nxiaolei".getBytes();
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//   \r\n  或 \n  换行符
//    \t  空格符

注意事项:

  • 流对象的构造方法:可以创建文件
  • 如果文件存在,文件中的内容会被覆盖

 2.2、字节输入流 InputStream

  • abstract int read()
  • int read(byte[] b)
  • int read(byte[] b, int off, int len)
  • close()
public class FileInputStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("e:\\test.txt");
            byte[] b = new byte[2];
            int read = 0;

            while ((read = fis.read(b)) != -1) {
                System.out.print(new String(b, 0, read));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

2.3、字节输入流InputStream

方法介绍:

  • int read()    从此输入流中读取一个数据字节
  • int read(byte[] b)   从此输入流中将最多b.length 个字节的数据读入一个byte数组中
  • int read(byte[] b, offset,int len)  从此输入流中将最多len个字节的数据读入一个byte数组中
public class FileInputStreamDeam {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\实训\\Demo\\src\\test\\abc.txt");
            int read = 0;
            while((read = fis.read()) != -1){
                System.out.println((char)read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis !=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2.4、字节流练习

使用读写操作完成文件的复制

(1)复制文件:

  • 原理:读取一个已有的数据,并将这些读到的数据写入
public class CopyFile {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\实训\\Demo\\src\\day0424\\dog.jpg");
            fos = new FileOutputStream("D:\\实训\\Demo\\src\\day0424\\copy.jpg");
            int read = 0;
            while((read = fis.read()) != -1){
                fos.write(read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

上述代码,每次都从源文件中读取一个字节,然后写入到指定文件中,接着再读取到下一个字节,然后再写入,这样效率较低。

(2)缓冲数组方式复制


            byte[] buf = new byte[1024];
            int read = 0;
            while((read = fis.read(buf)) != -1){
                fos.write(buf,0,read);   //偏移量:0  读取量 read
            }

三、字符流

3.1、字符编码表

编码表:生活中字符和计算机二进制的对应关系表

  • ASCII表
  • ISO-8859-1:拉丁码表
  • GB-2312:简体中文码 表
  • GBK:最常用的中文码表
  • Unicode:国际标准码表,用两个字节存储
  • UTF-8:基于Unicode,用一个字节存储

能够识别中文的码表:GBK、UTF-8

编码:文字---->数字

解码:数字---->文字


3.2、字符输入流Reader

方法:

  • int read()
  • int read(char[] cbuf)
public class ReaderDemo {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader("D:\\实训\\Demo\\src\\test\\abc.txt");
        int read = 0;
        while((read = reader.read())!=-1){
            System.out.println(read + " => "+(char)read);
        }
    }
}

3.3、字符输出流Write

方法:

  • write(char[] cbuf)
  • write(char[] cbuf,int off,int len)
  •  write(String str)
    public static void writeCN() throws IOException {
        FileWriter fw = new FileWriter("e:\\test.txt", true);
        fw.write(new char[] {'你', '好', '呀'});
        fw.flush();
        fw.close();
    }

3.4、 字符流复制文件

private static void conyByCharStream(String src, String copy) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(src);
            fw = new FileWriter(copy);
            int len = 0;
            char[] buf = new char[1024];
            while ((len = fr.read(buf)) != -1) {
                fw.write(buf, 0, len);
                fw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值