zoukankan      html  css  js  c++  java
  • BufferedReader和PrintWriter读写中文的问题

    最近用BufferedReader读一个文本文件,然后再将读出的内容用PrintWriter写入到另外一个新的文件中。

    之前一直没有发现这个问题,就是如果文本内容中有中文,在读出的内容和写入的内容都会出现乱码。

    想了一下解决方案。

    首先用BufferedReader读出的时候设置一下字符集编码:

    //文件路径:filePath
    File file = new File(filePath);
    FileInputStream fin = new FileInputStream(file);
    InputStreamReader in = new InputStreamReader(fin,"GBK");
    BufferedReader br = new BufferedReader(in);

     设置好了之后,会进行一些读出操作,然后再调用PrintWriter写入,当然也要设置同一个字符集编码:

    File tempFile = new File(newPath);
    PrintWriter tpw = null;
    FileOutputStream fo = new FileOutputStream(tmpFile);
    OutputStreamWriter osw = new OutputStreamWriter(fo,"GBK");
    tpw = new PrintWriter(osw);
  • 相关阅读:
    links[v1]
    WebSocket handshake: Unexpected response code: 404
    Spring mvc 启动 和 请求分发
    匹配括号
    js parseFloat 精度问题
    遍历查找跳格子逻辑
    Generic type test java
    java高效判断素数
    从数组中取3个数全排列
    vue-cli
  • 原文地址:https://www.cnblogs.com/xinmomoyan/p/11002217.html
Copyright © 2011-2022 走看看