字符流=字节流+编码表
Java为什么可以区分字母和汉字
package day3; import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; import java.util.Arrays; public class Test { public static void main(String[] args) throws UnsupportedEncodingException { String s1="abc"; String s2="你好"; byte[] byt1 = s1.getBytes(); byte[] byt2 = s2.getBytes(); byte[] byt3 = s2.getBytes("GBK"); System.out.println(Arrays.toString(byt1)); System.out.println(Arrays.toString(byt2)); System.out.println(Arrays.toString(byt3)); } }
我们发现中文都是负数 而英文都是正数
UFT-8中 三个字节对应一个中文字
GBK里面两个字节对应一个中文字
字符流的乱码现象
package day3; import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; import java.util.Arrays; public class Test { public static void main(String[] args) throws UnsupportedEncodingException { String s1 = new String("你好"); byte[] bytes = s1.getBytes("UTF-8"); String s2 = new String(bytes,"GBK"); System.out.println(s2); } }
为什么会产生乱码现象呢?
因为将字符串转换成字节数组的时候用的是UTF-8一个汉字三个字节
而将字节数组转换为字符的时候我们用的是GBK两个字节 一个汉字
当两者都是一样的时候不会乱码
package day3; import java.io.UnsupportedEncodingException; import java.lang.reflect.Array; import java.util.Arrays; public class Test { public static void main(String[] args) throws UnsupportedEncodingException { String s1 = new String("你好"); byte[] bytes1 = s1.getBytes("UTF-8"); String s2 = new String(bytes1,"UTF-8"); System.out.println(s2); byte[] bytes2 = s1.getBytes("GBK"); String s3 = new String(bytes2,"GBK"); System.out.println(s3); } }
*Java中默认采用UTF-8
字符流写数据的五种方式
-
void write(int c)写一个字符
-
void write(char[] cbuf)写入一个字符数组
-
void write(char[] cbuf,int off,int len)写入一个字符数组的一部分
-
voif write(String str)写入一个字符串
-
void (String str,int off,int len)写入一个字符串的一部分
package day3; import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("write.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write(97); osw.write(98); osw.write(99); osw.flush(); char[] c = {'\n','c','b','s'}; osw.write(c); osw.flush(); osw.write("\nhello\n"); osw.write("wworldd",1,5); osw.close(); } }
注意: 字符流写完要输出刷新 不然文本上不会有显示 close()是先刷新再关闭 所有close()有刷新的功能
字符流读数据
-
int read()一次读一个数据
-
int read(char[] cbuf)一次读一个字符串数组
package day3; import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("write.txt"); InputStreamReader isr = new InputStreamReader(fis); int r; while ((r=isr.read())!=-1){ System.out.print((char)r); } char[] c = new char[1024]; isr.close(); } }
package day3; import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("write.txt"); InputStreamReader isr = new InputStreamReader(fis); char[] c = new char[1024]; int len; while ((len=isr.read(c))!=-1){ System.out.println(new String(c,0,len)); } isr.close(); } }
两种读的方式
字符缓冲流
BufferedWriter 将文本写入字符输出流中
BufferedReader 从字符输入流读取文本
package day3; import java.io.*; public class Test { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("write.txt"); BufferedWriter bw = new BufferedWriter(fw); bw.write("hello\n"); bw.flush(); FileReader fr = new FileReader("write.txt"); BufferedReader br = new BufferedReader(fr); char[] ch = new char[1024]; int len; while ((len=br.read(ch))!=-1){ System.out.println(new String(ch,0,len)); } br.close(); bw.close(); } }