【发布时间】:2015-11-09 04:31:24
【问题描述】:
所以我正在编写一个程序,该程序将从文件中读取输入,将所有 html 标记替换为 >、大于和
public void rewrite(String from, String to) {
File f = new File(from);
File t = new File(to);
try {
Scanner s = new Scanner(f);
FileWriter fw = new FileWriter(to);
while (true) {
if (s.hasNext()) {
String a = s.next();
if (a.contains("<") || a.contains(">") || a.contains("&")) {
a.replace("<", "<,");
a.replace(">",">,");
a.replace("&","amp;,");
}
fw.append(a);
} else {
break;
}
}
fw.close();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
编辑:It is a warning not a compile time error nor a runtime error
【问题讨论】:
-
包含确切的错误信息怎么样?运行时还是编译时?
-
看起来应该可以,但您可能想尝试使用
String.replaceAll()而不是String.replace() -
您是否尝试在替换前后打印字符串'a'...