读书人

将整个工程转化编码格式的步骤

发布时间: 2013-09-25 11:02:58 作者: rapoo

将整个工程转化编码格式的方法

我们编程的时候经常遇到这样的问题,我们打开Eclipse就创建了工程开始编辑,但是往往做到一半的时候,被提示按照规定编码格式需要统一为UTF-8。

但是我们的Eclipse默认是选择GBK的,这时候有的人就会一个一个的用其他工具打开,然后粘贴到Eclipse里面替换掉原文件。这样确实不错,但是文件太多时就显得比较麻烦了,而且这也不符合程序员的特点(将一切事情尽可能程序化)。

今天我也遇到了这样的问题,于是简单的写了一个2个java类,实现了该功能,和大家分享一下吧。BUG肯定是有的,但是暂时我还没发现,暂时转化的还都是正常的。

package com.lxl.helper;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;public class IOHelper {/** * 把以一种编码格式的输入流转化为另一种编码格式的输出流 * @return */public static void fromIsToOsByCode(InputStream is,OutputStream os,String incode,String outcode){BufferedReader reader=null;BufferedWriter writer=null;try { reader = new BufferedReader(new InputStreamReader(is,incode)); writer= new BufferedWriter(new OutputStreamWriter(os, outcode)); String line; while((line=reader.readLine())!=null){ writer.write(line+"\n"); }} catch (Exception e) {e.printStackTrace();}finally{try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * 以指定的格式来读取输入流 */public static String readStrByCode(InputStream is,String code){StringBuilder builder=new StringBuilder();BufferedReader reader=null;try { reader = new BufferedReader(new InputStreamReader(is,code)); String line; while((line=reader.readLine())!=null){ builder.append(line+"\n"); }} catch (Exception e) {e.printStackTrace();}finally{try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return builder.toString();}/** * 输入InputStream流,返回字符串文字。 * @param is * @return */public static String fromIputStreamToString(InputStream is){if(is==null)return null;ByteArrayOutputStream baos = new ByteArrayOutputStream();int i = -1;try {while ((i = is.read()) != -1) {baos.write(i);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return baos.toString();}/** * 输入InputStream流和文件地址,返回成功与否。 * @param is * @return */public static boolean fromIputStreamToFile(InputStream is,String outfilepath){byte[] b=new byte[1024];FileOutputStream fos=null;try {fos=new FileOutputStream(new File(outfilepath));while((is.read(b, 0, 1024))!=-1){fos.write(b);}fos.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}finally{try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return true;}/** * 输入文件地址,返回inputStream流。 * @param is * @return */public static InputStream fromFileToIputStream(String infilepath){FileInputStream fis=null;try {fis=new FileInputStream(new File(infilepath));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return fis;}public static InputStream fromStringToIputStream(String s) {    if (s != null && !s.equals("")) {        try {            ByteArrayInputStream stringInputStream = new ByteArrayInputStream(                    s.getBytes());            return stringInputStream;        } catch (Exception e) {            e.printStackTrace();        }    }    return null;}/** * 把输入流转化成UTF-8格式的输入流 * @param in * @param charset * @return * @throws Exception *///public static InputStream fromInputStreamToInputStreamInCharset(//InputStream in, String charset) throws Exception {//StringBuilder builder=new StringBuilder();//byte[] buffer = new byte[2048];//int len = -1;//while ((len = in.read(buffer)) != -1) {//builder.append(EncodingUtils.getString(buffer, 0, len, "UTF-8"));//}//return IOHelper.fromStringToIputStream(builder.toString());//}public static InputStream getInputStreamFromUrl(String urlstr){try {InputStream is = null;HttpURLConnection conn = null;System.out.println("urlstr:"+urlstr);URL url = new URL(urlstr);conn = (HttpURLConnection) url.openConnection();if (conn.getResponseCode() == 200) {is = conn.getInputStream();return is;}} catch (Exception e) {System.out.println(e.toString());}return null;}public static void writerStrByCode(FileOutputStream os,String outcode,String str) {BufferedWriter writer=null;try { writer= new BufferedWriter(new OutputStreamWriter(os, outcode)); writer.write(str); writer.flush();} catch (Exception e) {e.printStackTrace();}}}


读书人网 >编程

热点推荐