首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

java读取写下文件指定编码

2012-08-09 
java读取写入文件指定编码String charEncoding UTF-8String path request.getRealPath(/body/js/d

java读取写入文件指定编码

String charEncoding = "UTF-8";        String path = request.getRealPath("/body/js/district.js");    StringBuilder sbl = new StringBuilder();    String end = "";    InputStreamReader isr = null;    BufferedReader bufferedReader = null;        try {            isr = new InputStreamReader(new FileInputStream(path), charEncoding);                        bufferedReader = new BufferedReader(isr);            if ((end = bufferedReader.readLine()) != null) {            sbl.append(end);            }        } catch (FileNotFoundException ex) {ex.printStackTrace();        ex.printStackTrace();        } catch (IOException ex) {            ex.printStackTrace();        }finally{        if(isr != null){        try {        isr.close();bufferedReader.close();} catch (IOException e) {e.printStackTrace();}        }        if(bufferedReader != null){        try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}        }        }                String newStr = AddressDeal.getNewPcdStr();    String content = sbl.toString();    int start = content.indexOf("var allDists=");    String newCont = content.substring(0,start);    newCont += "var allDists="+newStr+";";        FileOutputStream fos = null;    OutputStreamWriter writer= null;        try {        fos = new FileOutputStream(new File(path)) ;                       writer = new OutputStreamWriter(fos,charEncoding);             writer.write(newCont);        }catch (IOException ex) {          ex.printStackTrace();        }finally{        if(writer != null){        try {        writer.close();} catch (IOException e) {e.printStackTrace();}        }        if(fos != null){        try {        fos.close();} catch (IOException e) {e.printStackTrace();}        }        }
?

热点排行