按指定编码方式读取文件和输出文件

如果需要转载,请注明出处和作者。谢谢
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的编码方式保存的.如果不按指定编码方式读取的话生成出来的另外一个文件中的中文就会参生乱码.以上的编码方式可按自己的需要做改动.这只是个例子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值