javaio流的学习经验

本文介绍了Java IO流的概念,包括字节流和字符流的区分,以及如何根据文件类型选择合适的流进行读写操作。内容涵盖了File流、缓冲流的使用,以及转换流在不同编码间的转换。还讨论了数据流、打印流和编码表如ASCII、GBK、Unicode等的相关知识。

javaIO流
流按照数据单位的不同可以分为字节流(8bit),字符流(16bit)
按照数据的流向分为输入流和输出流

字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

javaIO流有40多个类,都是从上面的这四个抽象基类(不能实例化)中派生出来的
字符流操作的基本单位是字符,可以处理文字什么的,字节流处理图片
这四个类派生出来的子类名称是以父类的名作为子类的后缀
比如说文件流就是在这四个前加上File,也叫节点流,顾名思义,用来处理文件的
还有缓冲流(处理流的一种)是在名字前面加上Buffered

抽象基类节点流(文件流)缓冲流
InputStreamFileInputStream (read(byte[] buffer))BufferedInputStream (read(byte[] buffer))
OutputStreamFileOutputStream (read(byte[] buffer,0,len))BufferedOutputStream (read(byte[] buffer,0,len))
ReaderFileReader (read(char[] cbuf))BufferedReader (read(char[] cbuf)) /readlen()
WriterFileWriter (read(char[] cbuf,0,len))BufferedWriter

一般来说,文本文件(.text,.java,.c,.cpp)用字符流处理
对于非文本文件(.jpg,.mp3,.mp4,.doc,.ppt)用字节流处理

File流的应用
首先使用FileReader和FileWriter进行字节的读写

public void testFileReader() throws IOException {
        //1.实例化file对象,指明操作的文件
        try{
        File file = new File("helli.txt");
        //2.提供具体的流
        FileReader fr = new FileReader(file);
        //3.数据的读入,读入一个char字符
        int data = fr.read();
        while ((data=fr.read())!=-1){
            System.out.println((char)data);
        }
        }catch(IOException e){
             e.printStackTrace();
        }finally{
        //4.关闭流
        try{
        fr.close();
        }catch(IOException e){
              e.printStarkTrace();
        }
        }
    }

这里把东西读进来了
这里read()中也可以放一个char[]数组,相当于规定一次读多长

public void testFileRWriter() throws IOException {
        //1.实例化file对象,指明写出到的文件
        File file = new File("helli.txt");
        //2.提供具体的流
        FileWriter fw = new FileWriter(file);
        //3.数据的写出
        fw.write("I have a dream");
        fw.write("I have a dream too");
        //4.关闭流
        fw.close();
    }

这里是写文件的操作

public void testFileRWriterReader() throws IOException {
        //1.实例化file对象,指明和读入写出到的文件
        File srcFile = new File("hello1.txt");
        File destFile = new File("hello2.txt");
        //2.提供具体的流
        FileWriter fw = new FileWriter(destFile);
        FileReader fr = new FileReader(srcFile);
        //3.数据的写出
       char[] cbuf = new char[5];
       int len ;
       while ((len=fr.read(cbuf)) != -1)
           fw.write(cbuf,0,len);
        //4.关闭流
        fr.close();
        fw.close();
    }

将文件进行复制
但是呢,字符流不能进行图片或者视频的复制,需要用字节流

public void testFileRWriterReader() throws IOException {
        //1.实例化file对象,指明和读入写出到的文件
        File file = new File("hello1.txt");
        //2.流
        FileInputStream fis = new FileInputStream(file);
        //3.读
        byte [] buffer = new byte[5];
        int len ;
        while ((len = fis.read(buffer))!=-1){
            String str = new String(buffer,0,len);
            System.out.println(str);
        }
        //4.关
        fis.close();      
    }

注意,一个字母或数字占一个字节,但是一个汉字占三个字节,所以说,汉字用这种方法处理有可能出现乱码。
字节流进行文件的复制

public void testFileRWriterReader() throws IOException {
        //对图片进行复制操作
        //1.实例化file对象,指明和读入写出到的文件
        File srcFile = new File("hello1.jpg");
        File destFile = new File("hello2.jpg");
        //2.流
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //3.读
        byte [] buffer = new byte[5];//长度可以变
        int len ;
        while ((len = fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        //4.关
        fos.close();
        fis.close();
    }

Buffer流的应用
缓冲流就套在其他流的外边进行使用,是一个处理流

public void testFileRWriterReader() throws IOException {
        //对图片进行复制操作
        //1.实例化file对象,指明和读入写出到的文件
        File srcFile = new File("hello1.jpg");
        File destFile = new File("hello2.jpg");
        //2.1造节点流
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //2.2造缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3.读
        byte [] buffer = new byte[5];//长度可以变
        int len ;
        while ((len = bis.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
        //4.关(先关外边的,再关里边的)其实关外层流的时候内层也被关上了
        bis.close();
        bos.close();
        fos.close();
        fis.close();
    }

Buffer流内部提供8192的缓冲区,达到这么大之后一次性写出,所以速度很快。
bos.flush():清空缓冲区并输出

br = new BufferedReader(new FileReader(file));
//这里可以用readline()来读取一行,返回值为String或者空
String data;
while((data=br.readLine())!=null)

转换流
转换流也是处理流的一种,可以将字节流转化为字符流,属于字符流
包括InputStreamReader(将字节的输入流转化为字符的输入流)(解码)(97->‘a’)和OutputStreamWriter(将字符的输出流转化为字节的输出流)(编码)(‘a’->97)
下面是读的时候将字节流转化为字符输出

public void testFileRWriterReader() throws IOException {
        //对图片进行复制操作
        //1.实例化file对象,指明和读入写出到的文件
        File srcFile = new File("hello1.jpg");
        FileInputStream fis = new FileInputStream(srcFile);
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        char[] cbuf = new char[20];
        int len;
        while((len = isr.read(cbuf))!=-1)
        {
            String str = new String(cbuf,0,len);
            System.out.println(str);
        }
        isr.close();

    }

下面呢演示使用这两个转换流将utf-8文件转化为gbk文件

 public void testFileRWriterReader() throws IOException {
        //将一个文件从utf-8转化为gbk
       File file1 = new File("1.txt");
       File file2 = new File("2.txt");
       
       FileInputStream fis = new FileInputStream(file1);
       FileOutputStream fos = new FileOutputStream(file2);
       
       InputStreamReader isr = new InputStreamReader(fis,"utf-8");
       OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
       
       char[] cbuf = new char[20];
       int len;
       while ((len = isr.read(cbuf))!=-1){
           osw.write(cbuf,0,len);
       }
       
       isr.close();
       osw.close();

    }

输入输出流
System.out和System.in
System类中有setin()和setout()方法

public static void testFileRWriterReader() throws IOException {
         //System.in  --->   转换流 ---->  BufferedReader的readline()
        //因为System.in 返回的是字节流,我们需要用转换流将输入的字节流转化为字符流
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        while (true)
        {
            String data = br.readLine();
            System.out.println(data);
        }
    }

打印流
PrintStream和PrintWriter
如果用System.setOut()改变了输出的位置,就有可能不输出在控制台,输出在改变的文件里了
数据流
为了方便操作java中的基本数据类型和String
DataInputStream和DataOutputStream

每日一问
1.汉字使用什么方式进行存储?
字符集
常见的编码表:
ASCII:美国标准信息交换码,一个字节8位,他只用了七位,有128种情况,大小写字母和符号
下边的都是ANSI,美国国家标准学会,除了unicode,通常是平台的默认编码,例如英文操作系统是ISO8859-1,中文操作系统是GBK。
ISO8859-1:拉丁码表,一个字节的8位表示
GB2312:中国的中文编码表,最多两个字节表示所有字符(如果存字母啥的还是一个字节,一个字节首位是0的话就是一个字符)
GBK:中文的升级版编码表,存了更多的中文字符(简体和繁体),最多两个字节(如果存字母啥的还是一个字节,一个字节首位是0的话就是一个字符)
Unicode:国际标准码,融合了人类使用的所有字符,为每一个字符分配唯一的字符码,所有的字符都是两个字节。还是上边两个的问题。是utf-8,-16等等的统称。
UTF-8:也是国际标准的,1~6个字节,汉字使用3个字节来存,少数地区的字符才用4到6个字节存储
UTF-32:UTF-32 是固定长度的编码,始终占用 4 个字节,足以容纳所有的 Unicode 字符,所以直接存储 Unicode 编号即可,不需要任何编码转换。浪费了空间,提高了效率。
UTF-16:两个或者四个。
只有UTF-8 兼容 ASCII,UTF-32 和 UTF-16 都不兼容 ASCII,因为它们没有单字节编码。
char能存汉字?
char类型中存储的是Unicode编码,Unicode编码中是存在存在中文的,所以Char自然可以存储汉字
2.代码中,运算符两侧为什么要加空格?
一是为了美观,方便看
其次避免二义性

int i = j / *p;
int i=j/*p;

下边这个编译器就会以为是注释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值