IO流之-----字符流

字符流

1.字符流:
由于字节流操作中文不是特别方便,Java提供了字符流
字符流 = 字节流 + 编码表;
中文的字节存储方式:
汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数
编码表:
字符集:是一个系统支持的所有字符的集合,包括各个国家文字、标点符号、图形符号、
数字等;
计算机要准确的存储和识别各种字符集符号,就需要进行字符编码,一套字符集
必然至少有一套字符编码。常见字符集有ASCII字符集、GBK字符集、Unicode
字符集等。
常见的字符集:
ASCII字符集:基本的ASCII字符集,使用7位标识一个字符,共128字符。ASCII的
扩展字符集使用8位表示一个字符,共256字符,方便支持欧洲常用字符。是一个系
统支持的所有字符的集合,包括各个国家文字、标点符号、图形符号、数字等。
IASCII:是基于拉丁字母的一套电脑编码系统,用于显示现代英语,主要包括控制
字符(回车键、退格、换行键等)和可显示字符(英文大小写字符、阿拉伯数字和
西文符号)。
GBK字符集:
最常用的中文码表,是在GB2312标准基础上的扩展规范,使用了双字节编码方
案,共收录了21003个汉字,完全兼容GB2312标准,同时支持繁体汉字以继日
韩文字等。
Unicode字符集:
UTF-8 编码:可以用来表示Unicode标准中任意字符,它是电子邮件、网页及
其他存储或传送文字的应用中,优先采用的编码。互联网工程小组(IETF)
所有互联网协议都必须支持UTF-8编码。它使用一至四个字节为每个字符编
码。
编码规则:
128个US-ASCII字符,只需一个自己编码;
拉丁文等字符,需要二个字节编码;
大部分常用字(含中文),使用三个字节编码;
其他极少使用的Unicode辅助字符,使用四字节编码。
2.字符串中的编码节码问题
方法名 说明
byte[] getBytes() 使用平台的默认字符集将该 String编码为一系列字节
byte[] getBytes(String charsetName) 使用指定的字符集将该 String编码为一系列字节
String(byte[] bytes) 使用平台的默认字符集解码指定的字节数组来创建字符串
String(byte[] bytes, String charsetName) 通过指定的字符集解码指定的字节数组来创建字
符串
代码演示:
public static void main(String[] args) {
String s = “中国”;
// byte[] bytes = s.getBytes();//[-28, -72, -83, -27, -101, -67]
/* byte[] bytes = new byte[0];//[-28, -72, -83, -27, -101, -67]
try {
bytes = s.getBytes(“UTF-8”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
*/
byte[] bytes = new byte[0];
try {
bytes = s.getBytes(“GBK”);//[-42, -48, -71, -6]
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(bytes));
//String ss = new String(bys);
//String ss = new String(bys,“UTF-8”);
String ss = new String(bys,“GBK”);
System.out.println(ss);

}

3.字符流写数据
Writer:用于写入字符流的抽象父类;
FileWriter:用于写入字符流的常用子类。
构造方法:
方法名 说明
FileWriter(File file) 根据给定的 File 对象构造一个 FileWriter 对象
FileWriter(File file, boolean append) 根据给定的 File 对象构造一个 FileWriter 对象
FileWriter(String fileName) 根据给定的文件名构造一个 FileWriter 对象
FileWriter(String fileName, boolean append) 根据给定的文件名以及指示是否附加写入数
据的 boolean 值来构造 FileWriter 对象
成员方法:
方法名 说明
void write(int c) 写一个字符
void write(char[] cbuf) 写入一个字符数组
void write(char[] cbuf, int off, int len) 写入字符数组的一部分
void write(String str) 写一个字符串
void write(String str, int off, int len) 写一个字符串的一部分
刷新和关闭的方法:
方法名 说明
flush() 刷新流,之后还可以继续写数据
close() 关闭流,释放资源,但是在关闭之前会刷新流。一旦关闭,就不能再写数据
代码演示:
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter(“day12\testv\a.txt”);
//void write(int c):写一个字符
fw.write(97);
fw.write(98);
fw.write(99);
//void writ(char[] cbuf):写入一个字符数组
char[] chs = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’};
fw.write(chs);
//void write(char[] cbuf, int off, int len):写入字符数组的一部分
fw.write(chs, 0, chs.length);
fw.write(chs, 1, 3);
//void write(String str):写一个字符串
fw.write(“abcde”);
//void write(String str, int off, int len):写一个字符串的一部分
fw.write(“abcde”, 0, “abcde”.length());
fw.write(“abcde”, 1, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.字符流读数据
Reader:用于读取字符流的抽象父类;
FileReader:用于读取字符流的常用子类。
构造方法:
方法名 说明
FileReader(File file) 在给定从中读取数据的File的情况下创建一个新的FileReader
FileReader(String fileName) 在给定从中读取数据的文件名的情况下创建一个新 FileReader

成员方法:
方法名 说明
int read() 一次读一个字符数据
int read(char[] cbuf) 一次读一个字符数组数据
代码演示:
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader(“day12\testv\a.txt”);
char[] chs = new char[1024];
int len;
while ((len = fr.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
//int read():一次读一个字符数据
// int ch;
// while ((ch=fr.read())!=-1) {
// System.out.print((char)ch);
// }
//int read(char[] cbuf):一次读一个字符数组数据
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null){
//释放资源
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
5.字符缓冲流:
BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接收默认大小。默认值足够大,可用于大多数用途
BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取, 可以指定缓冲区大小,或者可以使用默认大小。默认值足够大,可用于大多数用途。
构造方法:
方法名 说明
BufferedWriter(Writer out) 创建字符缓冲输出流对象
BufferedReader(Reader in) 创建字符缓冲输入流对象
代码演示:
public static void main(String[] args) {
//BufferedWriter(Writer out)
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(“myCharStream\bw.txt”));
bw.write(“hello\r\n”);
bw.write(“world\r\n”);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//BufferedReader(Reader in)
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(“myCharStream\bw.txt”));
//一次读取一个字符数据
// int ch;
// while ((ch=br.read())!=-1) {
// System.out.print((char)ch);
// }
//一次读取一个字符数组数据
char[] chs = new char[1024];
int len;
while ((len=br.read(chs))!=-1) {
System.out.print(new String(chs,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.字符缓冲流特有功能
BufferedWriter:
方法名 说明
void newLine() 写一行行分隔符,行分隔符字符串由系统属性定义
BufferedReader:
方法名 说明
String readLine() 读一行文字。 结果包含行的内容的字符串,不包括任何行终
止字符如果流的结尾已经到达,则为null
代码演示:
public static void main(String[] args) {
// BufferedWriter bw = null;
// try {
// bw = new BufferedWriter(new FileWriter(“day12\testv\a.txt”));
// bw.write(“嘿嘿嘿”);
// bw.newLine();
// bw.write(“如果你能追到我”);
// bw.newLine();
// bw.write(“我就和你嘿嘿嘿”);
// bw.newLine();
// } catch (IOException e) {
// e.printStackTrace();
// }finally {
// if (bw != null){
// try {
// bw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(“day12\testv\a.txt”));
String line;
while ((line = br.readLine())!= null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值