编码与解码
编码就是把字符转换为字节,而解码是把字节重新组合成字符。
如果编码和解码过程使用不同的编码方式那么就出现了乱码。
- GBK 编码中,中文字符占 2 个字节,英文字符占 1 个字节;
- UTF-8 编码中,中文字符占 3 个字节,英文字符占 1 个字节;
- UTF-16be 编码中,中文字符和英文字符都占 2 个字节。
UTF-16be 中的 be 指的是 Big Endian,也就是大端。相应地也有 UTF-16le,le 指的是 Little Endian,也就是小端。
Java 的内存编码使用双字节编码 UTF-16be,这不是指 Java 只支持这一种编码方式,
而是说 char 这种类型使用 UTF-16be 进行编码。char 类型占 16 位,也就是两个字节,
Java 使用这种双字节编码是为了让一个中文或者一个英文都能使用一个 char 来存储。
String 的编码方式
String 可以看成一个字符序列,可以指定一个编码方式将它编码为字节序列,也可以指定一个编码方式将一个字节序列解码为 String。
String str1 = "中文";byte[] bytes = str1.getBytes("UTF-8");String str2 = new String(bytes, "UTF-8");System.out.println(str2);
在调用无参数 getBytes() 方法时,默认的编码方式不是 UTF-16be。
双字节编码的好处是可以使用一个 char 存储中文和英文,
而将 String 转为 bytes[] 字节数组就不再需要这个好处,因此也就不再需要双字节编码
。getBytes() 的默认编码方式与平台有关,一般为 UTF-8。
byte[] bytes = str1.getBytes();
public class StringDemo {public static void main(String[] args) throws UnsupportedEncodingException {String s = "你好";// String -- byte[]//byte[] bys = s.getBytes(); // [-28, -67, -96, -27, -91, -67]//byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]//byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]byte[] bys = s.getBytes("UTF-16be");//[79, 96, 89, 125]System.out.println(Arrays.toString(bys));// byte[] -- String//String ss = new String(bys); // 你好//String ss = new String(bys, "GBK"); // 你好//String ss = new String(bys, "UTF-8"); // 你好String ss = new String(bys, "UTF-16be"); //你好System.out.println(ss);}}
Reader 与 Writer
不管是磁盘还是网络传输,最小的存储单元都是字节,而不是字符。
但是在程序中操作的通常是字符形式的数据,因此需要提供对字符进行操作的方法。
- InputStreamReader 实现从字节流解码成字符流;
- OutputStreamWriter 实现字符流编码成为字节流。
InputStreamReader
- InputStreamReader的构造方法:
InputStreamReader(InputStream is) //用默认的编码读取数据InputStreamReader(InputStream is,String charsetName) //用指定的编码读取数据
public class ReaderDemo {public static void main(String[] args) throws IOException {InputStreamReader isr=new InputStreamReader(new FileInputStream("demo1.txt"),"UTF-8");int ch=0;while((ch=isr.read())!=-1){System.out.print((char)ch);}isr.close();}}
- 读入字符
int read() //一次读取一个字符int read(char[] chs) //一次读取一个字符数组
public class ReaderDemo2 {public static void main(String[] args) throws IOException {InputStreamReader isr=new InputStreamReader(new FileInputStream("demo1.txt"),"UTF-8");// 一次读取一个字符// int ch = 0;// while ((ch = isr.read()) != -1) {// System.out.print((char) ch);// }// 一次读取一个字符数组char[] chs = new char[1024];int len = 0;while ((len = isr.read(chs)) != -1) {System.out.print(new String(chs, 0, len));}isr.close();}}
OutputStreamWriter
- OutputStreamWriter的构造方法:
OutputStreamWriter(OutputStream out) //根据默认编码把字节流的数据转换为字符流OutputStreamWriter(OutputStream out,String charsetName) //根据指定编码把字节流数据转换为字符流//字符流 = 字节流 +编码表。
public class WriterDemo {public static void main(String[] args) throws IOException {OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("demo1.txt"),"UTF-8");//直接写字符osw.write("你好 hello\r\n");osw.close();}}
- 写出方法:
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) //写一个字符串的一部分
public class WriterDemo2 {public static void main(String[] args) throws IOException {OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("demo1.txt"),"UTF-8");// 写数据// public void write(int c):写一个字符osw.write('a');osw.write(97);// 为什么数据没有进去呢?// 原因是:字符 = 2字节// 文件中数据存储的基本单位是字节。// void flush()// public void write(char[] cbuf):写一个字符数组// char[] chs = {'a','b','c','d','e'};// osw.write(chs);// public void write(char[] cbuf,int off,int len):写一个字符数组的一部分// osw.write(chs,1,3);// public void write(String str):写一个字符串// osw.write("我爱林青霞");// public void write(String str,int off,int len):写一个字符串的一部分osw.write("我爱林青霞", 2, 3);// 刷新缓冲区osw.flush();// osw.write("我爱林青霞", 2, 3);// 释放资源osw.close();// java.io.IOException: Stream closed// osw.write("我爱林青霞", 2, 3);}}
注意:close()和flush()的区别?
- close()关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。
- flush()仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。
BufferedReader
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
public class ReaderDemo3 {public static void main(String[] args) throws IOException {// 创建字符缓冲输入流对象BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));// 方式1// int ch = 0;// while ((ch = br.read()) != -1) {// System.out.print((char) ch);// }// 方式2char[] chs = new char[1024];int len = 0;while ((len = br.read(chs)) != -1) {System.out.print(new String(chs, 0, len));}// 释放资源br.close();}}
BufferedWriter
public class WriterDemo3 {public static void main(String[] args) throws IOException {BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));bw.write("hello");bw.write("world");bw.write("java");bw.flush();bw.close();}}
字符缓冲流中的特殊方法
//BufferedWriter:public void newLine() //根据系统来决定换行符//BufferedReader:public String readLine() //一次读取一行数据//包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
/*** 字符缓冲流的特殊方法:* BufferedWriter:* public void newLine():根据系统来决定换行符* BufferedReader:* public String readLine():一次读取一行数据* 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null*/public class BufferDemo {public static void main(String[] args) throws IOException {//write();read();}public static void write() throws IOException {BufferedWriter bw=new BufferedWriter(new FileWriter("demo1.txt"));for (int x = 0; x < 3; x++) {bw.write("我爱林青霞");bw.newLine();bw.flush();}bw.write("重要的事情说三遍");bw.close();}public static void read() throws IOException {BufferedReader br=new BufferedReader(new FileReader("demo1.txt"));String line=null;while((line=br.readLine())!=null){System.out.println(line);}br.close();}}
字符转换流
public class TransferDemo {public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("demo4.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();}}
public class TransferDemo2 {public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("demo1.txt"));// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("demo4.txt"));String line=null;while((line=br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}// 释放资源bw.close();br.close();}}
复制文本文件
public class CopyFile {public static void main(String[] args) throws IOException{String srcFile="pride-and-prejudice.txt";String destFile="demo5.txt";long start=System.currentTimeMillis();//method(srcFile,destFile);//共耗时191毫秒//method2(srcFile,destFile);//共耗时59毫秒//method3(srcFile,destFile);//共耗时68毫秒//method4(srcFile,destFile);//共耗时38毫秒method5(srcFile,destFile);//共耗时177毫秒long end=System.currentTimeMillis();System.out.println("共耗时"+(end-start)+"毫秒");}//基本字符流一次读写一个字符public static void method(String srcFile,String destFile) throws IOException{FileReader fr=new FileReader(srcFile);FileWriter fw=new FileWriter(destFile);int ch=0;while((ch=fr.read())!=-1){fw.write(ch);}fr.close();fw.close();}//基本字符流一次读写一个字符数组public static void method2(String srcFile,String destFile) throws IOException{FileReader fr=new FileReader(srcFile);FileWriter fw=new FileWriter(destFile);int len=0;char[] chs=new char[1024];while((len=fr.read(chs))!=-1){fw.write(chs,0,len);}fr.close();fw.close();}//字符缓冲流一次读写一个字符public static void method3(String srcFile,String destFile) throws IOException{BufferedReader br=new BufferedReader(new FileReader(srcFile));BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));int ch=0;while((ch=br.read())!=-1){bw.write(ch);}br.close();bw.close();}//字符缓冲流一次读写一个字符数组public static void method4(String srcFile,String destFile) throws IOException{BufferedReader br=new BufferedReader(new FileReader(srcFile));BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));int len=0;char[] chs=new char[1024];while((len=br.read(chs))!=-1){bw.write(chs,0,len);}br.close();bw.close();}public static void method5(String srcFile,String destFile) throws IOException{BufferedReader br=new BufferedReader(new FileReader(srcFile));BufferedWriter bw=new BufferedWriter(new FileWriter(destFile));String line=null;while((line=br.readLine())!=null){bw.write(line);bw.newLine();bw.flush();}br.close();bw.close();}}
复制多级文件夹
public class CopyFolders {public static void main(String[] args) throws IOException {File srcFile=new File("demo2");File destFile=new File("demo3");copyFolder(srcFile,destFile);}//复制多级文件夹public static void copyFolder(File srcFile,File destFile) throws IOException {if(srcFile.isDirectory()){//在destFile下创建新文件夹File newDirectory=new File(destFile,srcFile.getName());newDirectory.mkdir();// 获取该File对象下的所有文件或者文件夹File[] files=srcFile.listFiles();if(files!=null){//将srcFile下的所有文件都复制到新destFilefor(File file:files){copyFolder(file,newDirectory);}}}else{File newFile=new File(destFile,srcFile.getName());copyFile(srcFile,newFile);}}//复制文件public static void copyFile(File srcFile,File destFile) throws IOException {BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));int len=0;byte[] bys=new byte[1024];while((len=bis.read(bys))!=-1){bos.write(bys,0,len);}bos.close();bis.close();}}
