场景:把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();
我现在想不管写的什么内容都把文件格式设置为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();
不是很确定,你试试吧。
我这里改成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>