输入输出流的读取输出 .txt 文件的 中文乱码问题 未解决

package interview;

import java.io.*;

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/83414120