1. 程式人生 > >輸入輸出流的讀取輸出 .txt 檔案的 中文亂碼問題 未解決

輸入輸出流的讀取輸出 .txt 檔案的 中文亂碼問題 未解決

package interview;

public class TestInOrOutStream {
public static void main(String[] args) {
    int c;

    try {
        InputStream is = new MyOwnInputStream(new BufferedInputStream(new FileInputStream("G://《國富論》全本.txt")), "UTF-8");
        while ((c = is.read()) >= 0) {
            System.out.print((char) c);


        }
        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;
        }
    }
}}

在這裡插入圖片描述