javaAPI_IO流_字符流

字符流

1.字符流出现的原因以及思想
由于字节流操作中文不是特别方便,所以,java就提供了转换流。

字符流=字节流+编码表


2.编码概述以及常见的编码

(1).什么是编码表
由字符及其对应的数值组成的一张表

(2).常见的编码
ASCII:美国标准信息交换码。
用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表
用一个字节的8位表示。
GB2312:中国的中文编码表。
GBK:中国的中文编码表升级,融合了更多的中文文字符号。
GB18030:GBK的取代版本
BIG-5码 :通行于台湾、香港地区的一个繁体字编码方案,俗称“大五码”。
Unicode:国际标准码,融合了多种文字。
所有文字都用两个字节来表示,Java语言使用的就是unicode
UTF-8:最多用三个字节来表示一个字符。(能够用一个表示的就用一个表示,如果不行,那么就用俩个,再不行就用三个),UTF-8不同,它定义了一种“区间规则”,这种规则可以和ASCII编码保持最大程度的兼容:


3.字符串中的编码问题

(1).相关方法
String(byte[] byte,String charsetName);通过指定的字符集解码字节数组。
byte[] getBytes(String charsetName);使用指定的字符集合把字符串编码为字节数组。

(2).编码以及解码概念
编码:把看得懂的变成看不懂的
解码:把看不懂的变成看得懂的

(3).一个字符串的编码和解码案例:

public static void main(String[] args) throws UnsupportedEncodingException {
//编码
String str = "你好";
//默认编码使用GBK
byte[] bys = str.getBytes();
System.out.println(Arrays.toString(bys));
System.out.println("-----------");
//解码
String str1 = new String(bys,"GBK");
System.out.println(str1);

}


4.OutputStreamWriter字符输出流

(1).构造方法
public OutputStreamWriter(OutputStream out):使用默认的编码的字符流写人数据
public OutputStreamWriter(OutputStream out,String charsetName):指定编码格式字符流写入数据

注意:字符流 = 字节流 + 编码表

(2).其他相关方法
public void write(int c):写一个字符
public void write(char[] cbuf):写一个字符数组
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
public void write(String str):写一个字符串
public void write(String str,int off,int len):写一个字符串的一部分

 

(3).OutputStreamWriter的基本使用举例
public static void main(String[] args) throws IOException {
//创建对象
OutputStreamWriter ost = new OutputStreamWriter(new FileOutputStream("test.txt"));
//写数据
ost.write("你好,胖子");
ost.close(); 
}


(4).字符输出流其他写入方法使用举例
public static void main(String[] args) throws IOException {
//创建对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("test.txt"));
osw.write('小');
//刷新缓冲区,因为字符其实存储的是在字符缓冲区里面,没有直接到文件里面,所以需要刷新一下
osw.flush();
osw.close();
}

(5).flush()和close()的区别
flush():仅仅刷新缓冲区,刷新之后流对象还可以继续使用。 
close():关闭流对象,但是先刷新一次缓冲区,这一个时候流对象不可以再使用了。


5.InputStreamReader 字符输入流

(1).构造方法
public InputStreamReader(InputStream in):使用默认编码读取数据
public InputStreamReader(InputStream in,String charsetName):指定编码读取数据

(2).其他相关方法 
public int read():读取单个字符
public int read(char[] cubf):将字符读入数组


(3).InputStreamReader的基本使用举例
public static void main(String[] args) throws IOException {
//创建对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"));

//读取数据:一次读取一个字符
/* int ch = 0;
while((ch = isr.read()) != -1){
System.out.print((char) ch);
}*/

System.out.println("------------------");
//一次读取一个字符串
char[] chs = new char[1024];
int len = 0;
while((len = isr.read(chs)) != -1){
System.out.print(new String(chs,0,len));
}

isr.close();
}



6.字符流复制文本文件案例

/*
* 需求:把c:\\a.txt内容复制到d:\\b.txt中

* 数据源:
* c:\\a.txt -- FileReader
* 目的地:
* d:\\b.txt -- FileWriter
*/
public class CopyFileDemo3 {
public static void main(String[] args) throws IOException {
// 封装数据源
FileReader fr = new FileReader("c:\\a.txt");
// 封装目的地
FileWriter fw = new FileWriter("d:\\b.txt");

// 读写数据
// int ch = 0;
int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}

//释放资源
fw.close();
fr.close();
}
}


7.BufferedWriter

(1).概述
字符流为了高效的读写文件,也提供了相应的字符缓冲流。

(2).BufferedWriter的基本使用
public static void main(String[] args) throws IOException {
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));

// 方式1
// int ch = 0;
// while ((ch = br.read()) != -1) {
// System.out.print((char) ch);
// }

// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}

// 释放资源
br.close();
}

(3)字符缓冲流的特殊功能
public void newLine():根据系统来决定换行符

8.BufferedReader

(1).概述
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了

(2).BufferedReader基本使用
public static void main(String[] args) throws IOException {
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));

// 方式1
// int ch = 0;
// while ((ch = br.read()) != -1) {
// System.out.print((char) ch);
// }

// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}

// 释放资源
br.close();
}

(3)字符缓冲流的特殊功能
public String readLine():一次读取一行数据,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null


(4).字符缓冲流特殊方法测试
public static void main(String[] args) throws IOException {
// write();
read();
}

private static void read() throws IOException {
// 创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("bw2.txt"));

// public String readLine():一次读取一行数据
// String line = br.readLine();
// System.out.println(line);
// line = br.readLine();
// System.out.println(line);

// 最终版代码
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}

//释放资源
br.close();
}

private static void write() throws IOException {
// 创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("bw2.txt"));
for (int x = 0; x < 10; x++) {
bw.write("hello" + x);
// bw.write("\r\n");
bw.newLine();
bw.flush();
}
bw.close();
}
 


9.字符缓冲流复制文本文件案例

版本1

/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

// 两种方式其中的一种一次读写一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}

// 释放资源
bw.close();
br.close();
}
}

//----------------------------------------------------------

版本2:[常用,更高效]

/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中

* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));

// 读写数据
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}

// 释放资源
bw.close();
br.close();
}
}

 

10.字符流小结

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值