场景:把old.html里的内容写到new.html里 如果内容是中文写出来的 文件格式为UTF-8,是英文就是ANSI
我现在想不管写的什么内容都把文件格式设置为UTF-8
还有一个问题就是现在写出来的内容是乱码,如果是中文。Test des = new Test();
// File file = new File("D:\\test\\1353294559812.html");
// InputStream in = null;
String fileName = "D:\\test\\old.html";
String fileName1 = "D:\\test\\new.html";
File file1 = new File(fileName1); try {
String inStr;
BufferedReader inn = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
FileOutputStream fout = new FileOutputStream(file1);
OutputStreamWriter writer = new OutputStreamWriter(fout, "UTF-8");
while ((inStr = inn.readLine()) != null) {
String outStr = new String(inStr.getBytes("UTF-8"), "UTF-8");
System.out.println(outStr);
writer.write(outStr);
}
// String str2 = des.encrypt("4578945475400000",outStr);
// System.out.println(str2); writer.flush();
writer.close();
// fout.close();
inn.close();

解决方案 »

  1.   

    读入的时候用BG2312试一试,写出来的时候用UTF-8.
    不是很确定,你试试吧。
      

  2.   

    刚才把你的程序我跑了一遍试了试,
    我这里改成GB2312就没有问题了。。
    new.html里面的内容<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>主页面</title>
    </head>
    <body>
    <p>主页面</p>
    <p><a href="manage.html">用户管理</a></p>
    <p><a href="add.html">用户添加</a></p>
    <p><a href="select.html">用户查找</a></p>
    </body>
    </html>
      

  3.   

    用DataInputStream 和 DataOutputStream里有read/write UTF应该可以满足楼主需求。