java调用python返回乱码

今天在使用java执行python文件时,因为python文件中输出的是中文,在Java控制台中全为乱码。

代码:

import org.python.util.PythonInterpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
*@author chenmeiqi
*@version 2020年2月26日 下午7:08:24
*/
public class test {

    public static void main(String[] args) throws IOException, InterruptedException {
        // TODO Auto-generated method stub 
        Process proc = Runtime.getRuntime().exec("D:\\Anaconda3\\envs\\py36\\python.exe D:/spider/ItemCF.py");
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
        proc.waitFor();
        System.out.println("end"); 
    }

}

运行结果:

 解决方法:

  在读取python输出窗口的信息时,只需添加一个参数gbk即可。

BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gbk"));

运行结果:

猜你喜欢

转载自www.cnblogs.com/qilin20/p/12369483.html