IO流——字符流

1、字符流读取读取字符的问题

 

读取F盘下的123.txt

 
 1 import java.io.*;
 2 import java.sql.SQLOutput;
 3 
 4 public class CharStreamDemo {
 5 
 6     public static void main(String[] args) throws IOException {
 7         readText();
 8     }
 9 
10     private static void readText() throws IOException {
11         File file = new File("F:\123.txt");
12         FileInputStream fis = new FileInputStream(file);
13         int len = 0;
14         while((len = fis.read()) != -1){
15             System.out.println(len);
16         }
17     }
18 
19 }

输出结果:

上面的程序在读取含有中文的文件时,并未输出具体的中文,而是输出了一些数字,这是为什么呢?

2、字符编码表

我们知道计算机底层数据存储的都是二进制数据,而我们生活中的各种各样的数据,如何才能和计算机中存储的二进制数据对应起来呢?
这时美国就把每一个字符和一个整数对应起来,就形成了一张编码表,这就是ASCII表。其中就是各种英文字符对应的编码。
编码表:其实就是生活中字符和计算机二进制的对应关系表。
1、ascii: 一个字节中的7位就可以表示。对应的字节都是正数。0-xxxxxxx
2、iso-8859-1:拉丁码表 latin,用了一个字节用的8位。1-xxxxxxx 负数。
3、GB2312:简体中文码表。包含6000-7000中文和符号。用两个字节表示。两个字节第一个字节是负数,第二个字节可能是正数
GBK:目前最常用的中文码表,2万的中文和符号。用两个字节表示,其中的一部分文字,第一个字节开头是1,第二字节开头是0
GB18030:最新的中文码表,目前还没有正式使用。
1、unicode:国际标准码表:无论是什么文字,都用两个字节存储。
 Java中的char类型用的就是这个码表。char c = 'a';占两个字节。
 Java中的字符串是按照系统默认码表来解析的。简体中文版 字符串默认的码表是GBK。
5、UTF-8:基于unicode,一个字节就可以存储数据,不要用两个字节存储,而且这个码表更加的标准化,在每一个字节头加入了编码信息(后期到api中查找)。
能识别中文的码表:GBK、UTF-8;正因为识别中文码表不唯一,涉及到了编码解码问题。
对于我们开发而言;常见的编码 GBK UTF-8 ISO-8859-1
文字--->(数字) :编码。 “abc”.getBytes() byte[]
(数字)--->文字 : 解码。 byte[] b={97,98,99} new String(b)

3、字符输入流Reader

上述程序中我们读取拥有中文的文件时,使用的字节流在读取,那么我们读取到的都是一个一个字节,只要把这些字节去查阅对应的编码表,就能够得到与之对应的字符。API中已经给我们提供了读取相应字符的功能流对象,Reader,读取字符流的抽象超类。

 
intread() 读取单个字符
int read(char[] cbuf) 将字符读入数组
 
 
 
 

read():读取单个字符并返回

read(char[] cbuf):将数据读取到数组中,并返回读取的个数。

3.1FileReader类

FileInputStream用于读取诸如图像数据之类的原始字节流,要读取字符流,请考虑使用FileReader。
构造方法

FileReader(File file)在给定从中读取数据的File情况下创建一个新的FileReader
FileReader(String FileName) 在给定从中读取数据的文件名的情况下创建一个新FileReader
 
 
 
 

3.2 FileReader读取包含中文的文件

1、单个字符读取

 1 import java.io.*;
 2 
 3 public class CharStreamDemo {
 4 
 5     public static void main(String[] args) throws IOException {
 6         readText();
 7     }
 8 
 9     private static void readText() throws IOException {
10         File file = new File("F:\123.txt");
11         FileReader reader = new FileReader(file);
12         int ch = 0;
13         while((ch = reader.read()) != -1){
14             System.out.print((char) ch);
15         }
16         reader.close();
17     }
18 
19 }
20  

2、字符数组读取

 1 import java.io.*;
 2 
 3 public class CharStreamDemo {
 4 
 5     public static void main(String[] args) throws IOException {
 6         readText();
 7     }
 8 
 9     private static void readText() throws IOException {
10         File file = new File("F:\123.txt");
11         FileReader reader = new FileReader(file);
12         int ch = 0;
13         char[] cbuf = new char[1024];
14         while((ch = reader.read(cbuf)) != -1){
15             System.out.print(new String(cbuf, 0, ch));
16         }
17         reader.close();
18     }
19 
20 }
 

4.字符输出流Writer

Writer是写入字符流的抽象类。

voidwrite(char[] cbuf) 写入字符数组
abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分
void write(int c) 写入单个字符
void write(String str) 写入字符串
void write(String str, int off, int len) 写入字符串的某一部分
 
 
 
 
 
 
 
 
 
 

4.1 FileWriter类

FileOutputStream用于写入诸如图像数据之类的原始字节的流,要写入字节流,请考虑使用FileWriter。
构造方法

FileWriter(File file)根据给定的File对象构造一个FileWriter对象。
FileWriter(File file, boolean append) 根据给定的File对象构造一个FileWriter对象。(当append的值为true时,可附加写入数据)
FileWriter(String fileName) 根据给定的文件名构造一个FileWriter对象
FileWriter(String fileName, boolean append) 根据给定的文件名以及指示是否附加写入数据的boolean值来构造FileWriter对象。
 
 
 
 
 
 
 
 
 
 
 
 
 
 

4.2 写入中文到文件中

写入字符到文件中,先进行流的刷新,再进行流的关闭。

 1 import java.io.*;
 2 
 3 public class CharStreamDemo {
 4 
 5     public static void main(String[] args) throws IOException {
 6         writeText();
 7     }
 8 
 9     private static void writeText() throws IOException {
10         File file = new File("F:\write.txt");
11         FileWriter writer = new FileWriter(file, true);
12         writer.write("你好谢谢再见");
13         writer.flush();
14         writer.close();
15     }
16 
17 }
18  

5. flush()和close()的区别

abstract voidclose() 关闭此流,但先要刷新它
abstract void flush() 刷新该流的缓冲
 
 
 
 
 

flush():将流中的缓冲区缓冲的数据刷新到目的地中,刷新后,流还可以继续使用。
close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,然后再关闭流。关闭后流不可以再使用。如果写入数据多,一定要一边写一边刷新,最后一次可以不刷新,由close完成刷新并关闭。

6.字符流练习:复制文本文件

 1 import java.io.*;
 2 
 3 public class CopyTextFileDemo {
 4 
 5     public static void main(String[] args) throws IOException {
 6         copyText();
 7     }
 8 
 9     private static void copyText() throws IOException {
10         File file = new File("F:\123.txt");
11         FileReader fr = new FileReader(file);
12         FileWriter fw = new FileWriter("F:\write.txt");
13         int len = 0;
14         char[] cbuf = new char[1024];
15         while((len = fr.read(cbuf)) != -1){
16             String str = new String(cbuf, 0, len);
17             fw.write(str);
18             fw.flush();
19         }
20         fr.close();
21         fw.close();
22     }
23 
24 }
原文地址:https://www.cnblogs.com/peng19920430/p/11581117.html