怎么样按指定编码方式读取文件和输出文件?
如果需要转载,请注明出处和作者。谢谢 QQ:221704 MSN:flyly@yeah.net EMAIL:zhangfl@sports.cn 例子说明:读取xml文件来生成一个xml格式的jsp文件 import java.io.*;
public class FileImp {
public static void main(String[] args) { FileImp flieimp = new FileImp(); try { BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream( "C://jbproject//image_test//modules.xml"), "UTF-8")); String str = new String(); String newStr = "<?xml version=/"1.0/" encoding=/"gb2312/"?>/r/n"; newStr += "<%@ page contentType=/"text/xml; charset=gb2312/" language=/"java/"%>/r/n"; str=in.readLine(); while ((str = in.readLine()) != null) { newStr += str; } in.close(); System.out.println(newStr); flieimp.write(newStr); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }}
public void write(String aString) {
try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C://jbproject//image_test//modules.jsp"),"GB2312")); out.write(aString); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } 这是在实际应用中遇到的一个问题.由于生成的文件是以UTF-8的编码方式保存的.如果不按指定编码方式读取的话生成出来的另外一个文件中的中文就会参生乱码.以上的编码方式可按自己的需要做改动.这只是个例子