字符流
一个汉字存储,GBK编码-占2个字节,UTF-8编码占3个字节
字符流= 字节流+编码表;
汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数
只能复制文本数据
编码
按某种规则,将字符存储到计算机中
解码:将计算机中的二进制数按照对应的规则解析显示出来。
字符编码:一套自然语言的字符与二进制数的对应关系
常见的字符集有:ASCII集:基于拉丁字母的一套电脑编码系统,用于显示现代英语,是一套系统支持的所有字符的集合
GBXXX系列字符集:如GB2312:两个大于127的字符组合在一起就表示一个汉字;
GBK:最常用的中文码表,收录了2W多个汉字,完全兼容GB2312;
GB18030:最新的中文码表,收录汉字7W多个,支持少数民族文字等。
Unicode字符集:为表达任意语言任意字符设计,是业界的一种标准,也称为统一码、标准万国码,有三种编码方案:UTF-8、UTF-16、UTF-32;
UTF-8编码:可以用与表示unicode中的任意字符,使用1-4个字节为每个字符编码,互联网工作工程小组要求所有互联网协议都支持UTF-8编码。
字符串中的编码解码问题
编码方法:
byte[] getBytes()(使用默认编码)
byte[] getBytes(String charsetName)
使用命名的字符集将该 String编码为一系列字节,将结果存储到新的字节数组中。
解码方法:
String(byte[] bytes)(使用默认解码)
String(byte[] bytes, Charset charset)
构造一个新的String由指定用指定的字节的数组解码charset
代码实例:
//编码
String s = "哈哈";
byte[] byt = s.getBytes("GBK");
System.out.println(Arrays.toString(byt));
//解码
String ss = new String(byt,"GBK");
System.out.println(ss);
字符流中的解码编码
字符流抽象基类:
Reader:字符输入流的抽象基类
Writer:字符输出流的抽象基类
字符流中和编码解码问题相关的两个类:
InputStringReader
OutputStringWriter
实例:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("1215/file.txt"),"UTF-8");
osw.write("哈哈");
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("1215/file.txt"),"UTF-8");
int ch;
while ((ch=isr.read()) != -1){
System.out.println((char)ch);
}
isr.close();
字符流写数据的5种方法
字符流写数据后,暂存在缓存区,在关闭输出流前要显示的话,要用刷新的方法flush写入文件中,flush后还可以继续写入
字符流读数据的2种方法
格式与字节流一样,只是一个是字节一个是字符
char[] cha = new char[1024];
int len;
while ((len = isr.read(cha)) != -1){
System.out.println(new String(cha,0,len));
}
isr.close();
FileWriter,FileReader
方便课写字符文件。 该类的构造函数假定默认字符编码和默认字节缓冲区大小是可以接受的。 要自己指定这些值,涉及到编码问题的,请在FileOutputStream上构造一个OutputStreamWriter。
FileReader,FileWriter(String fileName) 用于写入字符文件的便捷类,
构造一个给定文件名的FileWriter对象。
FileReader fr = new FileReader("1215/file.txt");
FileWriter fw = new FileWriter("1215/file1.txt");
char[] cha = new char[1024];
int ch;
while((ch=fr.read()) != -1){
fw.write(cha,0,ch);
}
fw.close();
fr.close();
字符缓冲流
BufferedReader
从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取。
BufferedWriter
将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入。
可以指定缓冲区大小,也可以使用默认大小
BufferedWriter bw = new BufferedWriter(new FileWriter("1215/file.txt"));
bw.write("yoyo");
bw.write("sosos");
bw.close();
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));
int len;
char[] cha =new char[1024];
while((len=br.read(cha)) != -1){
System.out.println(new String(cha,0,len));
}
while((len=br.read()) != -1){
System.out.print((char)len);
}
br.close();
字符缓冲流的特有功能:
//按整行读取(只读内容,不读换行的符号)
String line;
while ((line=br.readLine()) != null){
System.out.println(line);
}
复制文件
String line;
while ((line=br.readLine()) != null){
bw.write(line);
bw.newLine();
}
代码实例
文件到集合
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));
ArrayList<String> list = new ArrayList<>();
String line;
while((line=br.readLine())!= null){
list.add(line);
}
for(String s : list){
System.out.println(s);
}
抽奖小程序
BufferedReader br = new BufferedReader(new FileReader("1215/file.txt"));//file里按行写入名单
ArrayList<String> list = new ArrayList<>();
String line;
while((line=br.readLine())!= null){
list.add(line);
}
Random r = new Random();
int index = r.nextInt(list.size());
String luck = list.get(index);
System.out.println("the luck one is "+ luck);
br.close();