java基础之IO流之字符流
字符流
传输char和String类型的数据
输入流
-
抽象父类:Reader
-
节点流:FileReader
常用方法
-
int read():读取一个字符,读取到达末尾,返回-1
package com.by.test2;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestReader {public static void main(String[] args) {try(//字符输入节点流FileReader fr=new FileReader("file/d.txt")){while (true) {int n = fr.read();if (n == -1) {break;}System.out.println((char) n);}
}catch (FileNotFoundException e) {System.out.println("文件路径不正确");} catch (IOException e) {System.out.println("读写失败!");e.printStackTrace();} catch (Exception e) {System.out.println("未知异常!");e.printStackTrace();}}
}
输出流
-
抽象父类:Writer
-
节点流:FileWriter
常用方法
-
write(int ):向目标文件写入一个字符
-
write(String ):向目标文件写入一个字符串
package com.by.test2;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestWriter {public static void main(String[] args) {try(//字符输出节点流FileWriter fw=new FileWriter("file/e.txt")){fw.write(65);fw.write(66);fw.write(67);//写入字符串fw.write("一二三四五");fw.write("上山打老虎");
System.out.println("写入成功!");
}catch (FileNotFoundException e) {System.out.println("文件路径不正确");} catch (IOException e) {System.out.println("读写失败!");e.printStackTrace();} catch (Exception e) {System.out.println("未知异常!");e.printStackTrace();}}
}
输入缓冲过滤流
-
BufferedReader
方法
-
String readLine(): 一次读取一行数据,读取到达末尾返回null
package com.by.test2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class TestBufferedReader {public static void main(String[] args) {try(//字符输入节点流FileReader fr=new FileReader("file/d.txt");//添加缓冲过滤流BufferedReader br=new BufferedReader(fr)){while (true) {//接收本次读取内容String s = br.readLine();//判断读取是否到达末尾if (s == null) {break;}System.out.println(s);}
}catch ...}
}
输出缓冲过滤流
-
PrintWriter
-
BufferedWriter中的方法没有PrintWriter更多更实用
-
方法
-
print(值):向目标文件写入一个数据,默认不换行
-
println(值): 向目标文件写入一个数据,默认换行
-
println(): 向目标文件写入一个空行
package com.by.test2;
import com.by.entity.Student;
import java.io.*;
public class TestPrintWriter {public static void main(String[] args) {try(//字符输出节点流FileWriter fw=new FileWriter("file/e.txt");//添加缓冲过滤流PrintWriter pw=new PrintWriter(fw)){pw.print(5.5);pw.print(true);pw.println();pw.println("一二三四五");pw.println("上山打老虎");
Student s = new Student("zhangsan", 20, 99.5);pw.println(s);
System.out.println("写入成功!");
}catch...}
}
pw对象中的print|println方法写对象与对象过滤流写对象的区别?
pw只是在读写对象引用的toString方法的结果,并未读写对象的完整信息,所以无法对对象进行序列号及反序列化
对象过滤流是在读写对象的完整信息,所以可以对对象进行序列号及反序列化
pw中的print|println方法与输出语句中的有何不同?
pw是将数据写入到目标文件长久保存
标准输出流是将数据写入到控制台临时查看
字符编码集
-
编码: 原内容–>加密–>数字
-
解码: 数字–>解密–>原内容
常见编码集
-
GBK: 简体中文
-
Big5: 繁体中文
-
ASC||: 美国
-
ISO-8859-1: 西欧
-
Unicode:
-
UTF-16: java默认编码集,所有内容统一占用2个字节
-
UTF-8: 行业标准, 所占字节由内容大小决定,通常为1-3个字节
-
不同编码集拥有独立的编解码方式,之间互不相通
桥转换流
-
输入: InputStreamReader
-
输出: OutputStreamWriter
创建
-
基于字节节点流对象. 将字节流中的内容转换为字符流,并在转换过程中设置数据传输的编码集
InputStreamReader isr=new InputStreamReader(fis对象,"编码集"); OutputStreamWriter osw=new OutputStreamWriter(fos对象,"编码集");
步骤
-
创建字节节点流,声明被操作的文件路径
-
创建桥转换流,声明操作文件时所用的编码集
-
添加缓冲过滤流,提高数据操作的便捷性
使用
对同一文件的输入输出操作必须保证编解码集一致
package com.by.test2;
import java.io.*;
public class Test_ISR_OSW {public static void main(String[] args) {try(//创建输出流//创建字节输出节点流FileOutputStream fos=new FileOutputStream("file/m.txt");//创建桥转换流OutputStreamWriter osw=new OutputStreamWriter(fos,"GBK");//添加缓冲过滤流PrintWriter pw=new PrintWriter(osw);
//创建输入流BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("file/m.txt"),"GBK"))
){//先写pw.println("一二三四五");pw.println("上山打老虎");pw.println("老虎没打着");pw.println("打着小松鼠");//刷新缓冲区pw.flush();//后读while (true) {String s = br.readLine();if (s == null) {break;}System.out.println(s);}}catch...}
}
掌握
-
缓冲过滤流的使用
-
桥转换流的创建和使用