输入输出流读取本机txt文档:不能将中文字符流转化为char 因为char一个字节,中文在UTF-8的情况下是三个字节,会出现字符流截取
package interview;
import org.junit.Test;
import java.io.*;
public class TestInOrOutStream {
class m {
@Test
void x(){
System.out.println("kkk");
}
}
public static void main(String[] args) {
String i;
BufferedReader is = null;
try {
String file = "G://《国富论》全本.txt";
is = new BufferedReader(new FileReader(file));
// InputStream is = new MyOwnInputStream(new BufferedInputStream(new FileInputStream("G://《国富论》全本.txt")), "UTF-8");
// InputStream is = new MyOwnInputStream(new BufferedInputStream(new FileInputStream("G://国富论(英文版).txt")), "UTF-8");
while (( i = is.readLine()) != null) {
//不能将中文字符流转化为char 因为char一个字节,中文在UTF-8的情况下是三个字节,会出现字符流截取
System.out.print(i);
}
System.out.println("中文乱码没有解决");
is.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
static class MyOwnInputStream extends FilterInputStream {
public MyOwnInputStream(InputStream in, String s) {
super(in);
}
public int read() throws IOException {
int c = 0;
if ((c = super.read()) != -1) {
if (Character.isLowerCase((char) c))
return Character.toUpperCase((char) c);
else if (Character.isUpperCase((char) c))
return Character.toLowerCase((char) c);
else {
return c;
}
} else {
return -1;
}
}
}}