java 字符流 utf8,JAVA基础(字符流设置编码读写字符)
1,使用指定的碼表讀寫(xiě)字符
FileReader是使用默認(rèn)碼表讀取文件, 如果需要使用指定碼表讀取, 那么可以使用InputStreamReader(字節(jié)流,編碼表)
FileWriter是使用默認(rèn)碼表寫(xiě)出文件, 如果需要使用指定碼表寫(xiě)出, 那么可以使用OutputStreamWriter(字節(jié)流,編碼表)
【1】?代碼分析
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Demo7_TransIO {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//demo1();
//demo2();
BufferedReader br =?????????????????????????????????//更高效的讀
new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"), "utf-8"));
BufferedWriter bw =?????????????????????????????????//更高效的寫(xiě)
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk"));
int c;
while((c = br.read()) != -1) {
bw.write(c);
}
br.close();
bw.close();
}
public static void demo2() throws UnsupportedEncodingException,
FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"), "uTf-8");????//指定碼表讀字符
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"), "gbk");????//指定碼表寫(xiě)字符
int c;
while((c = isr.read()) != -1) {
osw.write(c);
}
isr.close();
osw.close();
}
public static void demo1() throws FileNotFoundException, IOException {
//用默認(rèn)編碼表讀寫(xiě),出現(xiàn)亂碼
FileReader fr = new FileReader("utf-8.txt");
FileWriter fw = new FileWriter("gbk.txt");
int c;
while((c = fr.read()) != -1) {
fw.write(c);
}
fr.close();
fw.close();
}
}
2,轉(zhuǎn)換流圖解
畫(huà)圖分析轉(zhuǎn)換流
標(biāo)簽:字符,java,import,讀寫(xiě),io,new,close,JAVA,碼表
來(lái)源: https://blog.csdn.net/Cricket_7/article/details/95076775
總結(jié)
以上是生活随笔為你收集整理的java 字符流 utf8,JAVA基础(字符流设置编码读写字符)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html5实例异步图片加载,javasc
- 下一篇: php获取ios,IOS 通过描述获取U