就一个秘密。ANSI其实就是GBK。
/**
* 向文件中写入内容
* @param filepath 文件路径与名称
* @param newstr 写入的内容
* 以ansi的编码格式打开文件
* @return
* @throws IOException
*/
private static boolean writeFileContent2(String filepath,String newstr) throws IOException{
Boolean bool = false;
String filein = newstr;//新写入的行
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
OutputStreamWriter pw = null;
try {
File file = new File(filepath);//文件路径(包括文件名称)
//将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
//文件原有内容
for(int i=0;(temp =br.readLine())!=null;i++){
buffer.append(temp);
// 行与行之间的分隔符 相当于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(filein);
fos = new FileOutputStream(file);
pw = new OutputStreamWriter(fos,"GBK");
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
logger.error(e.getMessage());
}finally {
//不要忘记关闭
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bool;
}