[JAVA基础]流文件读写文件编码转换

将本地的gbk文件流读取变换成utf-8文件

    public static void gbk2utf8(String targetFile,String outFile)
    {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try
        {
            InputStream is = new FileInputStream(targetFile);
            OutputStream os = new FileOutputStream(outFile);
            br = new BufferedReader(new InputStreamReader(is,"gbk"));
            String str = "";
            bw = new BufferedWriter(new OutputStreamWriter(os,"utf-8"));
            while((str = br.readLine())!=null)
            {
                bw.write(str);
                bw.newLine();   
            }
        
        } catch (Exception e)
        {
            e.printStackTrace();
        } 
        finally
        {
            try
            {
            
                if(null!=bw)
                    bw.close();
                if(null!=br)
                    bw.close();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

 

posted @ 2012-10-29 11:36  系咪噶  阅读(834)  评论(0编辑  收藏  举报