09-03
20

在Java中读写UTF-8编码文件的中文问题

昨天下午写代码,需要生成一个XML文件,以取代原来那种HTML的碎片方式。但是写出来的文件用浏览器打开后都是乱码。我已经在XML的最前面加上了
<?xml version="1.0" encoding="UTF-8"?>
而且浏览器的编码也是UTF-8的,这就排除了浏览器的问题。
再用VIM打开,发现用GB2312看是没问题的,换成:set encoding=UTF-8以后开始乱码
这时我尝试将字符串转码后写入文件,但在UTF-8,GBK和ISO8859_1中间怎么转也没有用。
忽然想起前几天yiyayoyo同学和我提过Java写文件默认编码的问题,于是开始google,发现我用的写文件的方式无法指定编码,于是换用另一种写文件的方式指定UTF-8,遂搞定。代码如下:

老代码:
PrintWriter pw = new PrintWriter(new FileWriter(path));
pw.print(content);
pw.close();

新代码:
FileOutputStream fos = new FileOutputStream(path);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(content);
out.close();
fos.close();


-------------------------------------------------

读代码也有编码的问题,如果要读取UTF-8的文件,应采用如下方式覆盖默认编码:
FileInputStream fis = new FileInputStream(s);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while ((line = d.readLine()) != null)
                    content.append(line + "\n");
d.close();
in.close();
fis.close();



注来自:http://my.lintun.com/article/1718



--------------------------代码(来自lib<李总>  未修改的)-----------------------

//package com.fileutil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

class FileUtil {
public static final String ENTER = System.getProperty("line.separator");

public static String readFileContent(String fullPath) {
  File f = new File(fullPath);
  if (f.isDirectory()) {
   return null;
  }
  StringBuffer sb = new StringBuffer();
  BufferedReader br = null;
  FileReader fr = null;
  String strLine = null;
  try {
   fr = new FileReader(fullPath);
   br = new BufferedReader(fr);
   while ((strLine = br.readLine()) != null) {
    sb.append(strLine + ENTER);
   }
   br.close();
   fr.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return sb.toString();
}

public static String readFileContentByByte(String fullPath) {
  File f = new File(fullPath);
  if (f.isDirectory()) {
   return null;
  }
  byte[] byteContent = new byte[0];
  byte[] buffer = new byte[4096];
  int iBeRead = -1;
  FileInputStream fis = null;
  String content = null;
  try {
   fis = new FileInputStream(fullPath);
   while ((iBeRead = fis.read(buffer, 0, 4096)) != -1) {
    byteContent = arrayAppendArray(byteContent, buffer, 0, iBeRead);
   }
   fis.close();
   content = new String(byteContent, "utf-8");
  } catch (Exception e1) {
   e1.printStackTrace();
  }

  return content;
}

public static byte[] arrayAppendArray(byte[] arrayDes, byte[] arraySrc,
   int offset, int length) {
  byte[] array = new byte[arrayDes.length + length];

  for (int i = 0; i < arrayDes.length; i++) {
   array[i] = arrayDes[i];
  }
  for (int i = arrayDes.length; i < arrayDes.length + length; i++) {
   array[i] = arraySrc[offset++];
  }
  arrayDes = null;
  arraySrc = null;
  return array;
}

public static void writeToFile(String fileContent, String fullPath) {

  try {
//   fileContent = new String(fileContent.getBytes("UTF-8")).trim();
   FileWriter fw = new FileWriter(fullPath);
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(fileContent);
   bw.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
}

public static void main(String args[]) throws Exception {}

}

public class Test {
public static void main(String args[])throws Exception{
  String src = "C:/BANK_ADD.jsp";
  String des = "C:/DESC_BANK_ADD.jsp";
  String desUTF_8 = "C:/UTF_8DESC_BANK_ADD.jsp";
  
  
  String content = FileUtil.readFileContent(src);
  FileUtil.writeToFile(content, des);
  
  
  
  String utf_8Content = new String(content.getBytes("gbk"),"utf-8");
  FileUtil.writeToFile(utf_8Content, desUTF_8);
}
}





文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: UTF-8
相关日志:
评论: 0 | 引用: 0 | 查看次数: 471
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭