读写文本文件时,在windows系统正常,在linux下出现乱码!
我用jsp读取一文本文件,然后替换里面指定的字符,在生成一个新的文本文件,在windows系统正常,在redhat linux as 3.0 下出现乱码!
文本文件 test.html 的内容只有一句话: 这里是日本的地方。
我用jsp读取后,替换掉里面的日本后生成一个新的 test1.html 文件.
jsp代码如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page language="java" %>
<%@ page import="java.io.*"%>
<%
String webroot=config.getServletContext().getRealPath("/");
String replace="中国";
String Str="",temp="";
try {
FileReader fr =new FileReader(webroot+"test.html");
BufferedReader bfr= new BufferedReader(fr);
while((temp=bfr.readLine())!=null){
Str+=temp+"\r\n";
}
fr.close();
Str=Str.replaceAll("日本",replace);
FileWriter fw =new FileWriter(webroot+"test1.html");
BufferedWriter bfw= new BufferedWriter(fw);
bfw.write(Str);
bfw.flush();
fw.close();
}catch (Exception e){
out.print(e.toString());
}
out.print("<br>"+Str);
%>