Java编码转换工具类

由于window和linux默认字符编码不同,所以某些与中文相关的操作,特别是xml数据,需要转码以适应不同操作系统。

工具类:

 /** 
	  * @param infile 源文件路径 
	  * @param outfile 输出文件路径 
	  * @param from 源文件编码 
	  * @param to 目标文件编码 
	  * @throws IOException 
	  * @throws UnsupportedEncodingException 
	  */  
	public static void convert(String infile, String outfile, String from,  String to) 
	{  
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile), from));  
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), to)));  
			String reading;  
			while ((reading = in.readLine()) != null)  
			{  
				out.println(reading);  
			}  
			out.close();  
			in.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码格式---"+e.getMessage());
		} catch (FileNotFoundException e) {
			System.out.println("文件不存在---"+e.getMessage());
		} catch (IOException e) {
			System.out.println("IO异常---"+e.getMessage());
		}  
	}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值