日期:2014-05-20 浏览次数:20792 次
byte[] bs = str.getBytes(); newstr = new String(bs, "UTF-8");
InputStreamReader read = new InputStreamReader(new FileInputStream(A.txt),"UTF-8"); BufferedReader br = new BufferedReader(read); BufferedWriter bw = new BufferedWriter(new FileWriter(B.txt));
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class FileProcess { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream(new File("d:\\a.txt")); fos = new FileOutputStream(new File("d:\\b.txt")); byte[] buffer = new byte[fis.available()]; fis.read(buffer); String str = new String(buffer, "GBK"); fos.write(str.getBytes("utf-8")); }catch(Exception e){ e.printStackTrace(); }finally{ if(fis != null){ try{ fis.close(); }catch(Exception e){ } } if(fos != null){ try{ fos.close(); }catch(Exception e){ } } } } }
------解决方案--------------------
我试了一下,好像没有问题,你看看
import java.io.BufferedReader; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; public class Test01 { public static void main(String[] args) { File in = new File("test.txt"); File out = new File("test2.txt"); BufferedReader br = null; OutputStreamWriter ow = null; try { br = new BufferedReader(new FileReader(in)); ow = new OutputStreamWriter(new FileOutputStream(out), "utf-8"); for(String str = null, separator = System.getProperty("line.separator"); (str = br.readLine()) != null;) { ow.write(str); ow.write(separator); } } catch (IOException e) { e.printStackTrace(); } finally { close(br); close(ow); } System.out.println("ok"); } private static void close(Closeable stream) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } }