java将字符串写入文件以及从文件读出字符串中文乱码解决

2020-03-11  本文已影响0人  haiyong6

今天在写一个字符串写入文件的案例的时候出现了中文乱码问题,奇怪的是在我电脑本地是不乱码的,但是在测试环境服务器上是乱码的,先贴出原代码

/**
     * 把string字符串写入文件
     * @param html
     * @param htmlPath
     * @param string
     * @return
     */
    public static boolean writeStrToFile(String str, String path, String fileName) {
        boolean success = false;
        File dir = new File(path);
        if(!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(path + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        byte bytes[] = new byte[1024 * 1024 * 200];
       bytes = str.getBytes();
       FileOutputStream fos;
          try {
            fos = new FileOutputStream(file);
            fos.write(bytes);
           fos.flush();
            fos.close();
            success = true;
          } catch (Exception e) {
           e.printStackTrace();
         }
         
        return success;
    }

以上代码把字符串转化成字节码写入到文件中,出现了中文乱码问题。

更改后代码 使用BufferedWriter解决了这个问题

/**
     * 把string字符串写入文件
     * @param html
     * @param htmlPath
     * @param string
     * @return
     */
    public static boolean writeStrToFile(String str, String path, String fileName) {
        boolean success = false;
        File dir = new File(path);
        if(!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(path + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     // 获取该文件的缓冲输出流
        BufferedWriter bufferedWriter = null;
        // 写入信息
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bufferedWriter.write(str);
            bufferedWriter.flush();// 清空缓冲区
            bufferedWriter.close();// 关闭输出流
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

总之,读写文件有关字符串的,用BufferedReader和 BufferedWriter比较方便

读文件:使用new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

        StringBuffer strBuf = new StringBuffer();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        int tempchar;
        while ((tempchar = bufferedReader.read()) != -1) {
            strBuf.append((char) tempchar);
        }
        bufferedReader.close();
        return strBuf.toString();

写文件:使用new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));

        File file = new File(filePath + "/" + fileName);
        if (!file.exists()) {// 如果文件不存在则创建
            file.createNewFile();
        }
        // 获取该文件的缓冲输出流
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        // 写入信息
        bufferedWriter.write(str);
        bufferedWriter.flush();// 清空缓冲区
        bufferedWriter.close();// 关闭输出流
上一篇 下一篇

猜你喜欢

热点阅读