Java学习笔记29(IO字符流,转换流)

时间:2019-03-25
本文章向大家介绍Java学习笔记29(IO字符流,转换流),主要包括Java学习笔记29(IO字符流,转换流)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

字符流:只能操作文本文件,与字节流的区别是,字节流是按照字节来读取文件,而字符流是按照字符来读取,因此字符流的局限性为文本文件

字符输出流:Write类,使用时通过子类   每一次写入都要刷新

package com.zs;

import java.io.FileWriter;
import java.io.IOException;

public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileWriter fw=new FileWriter("d:\\c.txt");
        fw.write(101);//输入数字自动编码
        fw.flush();//字符流每次操作都要使用flush方法刷新
        char[] ch={'a','b','c'};
        fw.write(ch);//输入数组,写字符数组
        fw.flush();
        fw.write(ch,0,2);//写部分字符数组选则的字符
        fw.write("hello java");//可以直接写字符串
        fw.close();
    }
}

字符输入流:Reader类,通过子类

package com.zs.Demo2;

import java.io.FileReader;
import java.io.IOException;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileReader fr=new FileReader("d:\\c.txt");
        int len=0;
        while((len=fr.read())!=-1){//一个字符一个字符的读
            System.out.println((char)len);
        }
        fr.close();
        FileReader fr1=new FileReader("d:\\c.txt");
        char[] c=new char[1024];//用字符数组读数据,加快速度
        while((len=fr1.read(c))!=-1){
           System.out.println(new String(c,0,len));
        }
        fr.close();
    }
}

复制文件:与字节流相似,需要注意每次写入后都要刷新

package com.zs.Demo2;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyFileByChar {
    public static void main(String[] args) {
        FileReader fr=null;
        FileWriter fw=null;
        try {
            fr=new FileReader("d:\\c.txt");
            fw=new FileWriter("e:\\c.txt");
            int len;
            char[] c=new char[1024];
            while((len=fr.read(c))!=-1){
                fw.write(c,0,len);
          fw.flush(); } }
catch (IOException e) { throw new RuntimeException("复制失败"); }finally { if (fw != null) { try { fw.close(); } catch (IOException e) { throw new RuntimeException("释放资源失败"); }finally { if (fr != null) { try { fr.close(); } catch (IOException e) { throw new RuntimeException("释放资源失败"); } } } } } } }

转换流:字符流和字节流之间的桥梁

OutputStreamWriter类:字符转字节

package com.zs.Demo2;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class CharToByte {
    public static void main(String[] args) throws IOException {
        FileOutputStream fo=new FileOutputStream("d:\\d.txt");
        //OutputStreamWriter(字节流对象,编码格式);
        OutputStreamWriter fw=new OutputStreamWriter(fo,"utf-8");
        fw.write("你好");//这里本来d盘时字节流输入的,可以使用字符流输入字符串;
        fw.close();
    }
}

InputStreamReader:字节转字符

package com.zs.Demo2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ByteToChar {
    public static void main(String[] args) throws IOException {
        FileInputStream fi=new FileInputStream("d:\\d.txt");
        InputStreamReader fr=new InputStreamReader(fi,"utf-8");
        char[] c=new char[1024];
        int len=0;
        while((len=fr.read(c))!=-1){
            System.out.println(new String(c,0,len));//你好
        }
    }
}

注意读取文本的编码格式要一致