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();
}
}
}
java读取写入文件指定编码
最新推荐文章于 2022-12-23 23:03:44 发布