-1
因此,我正在編寫一個程序,它將讀取來自文件的輸入,將所有html標記替換爲>之類的大於<,對於小於和小於我的邏輯是正確的,但由於某種原因,當我嘗試在'from'文件中的字符串上使用.replace方法時,它給了我一個警告,指出忽略了.replace方法。我嘗試閱讀ide告訴我的內容,但我不明白它說的是什麼。從文件中讀取字符串後替換字符java
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
如何包含確切的錯誤信息?運行時還是編譯時間? – John3136
,它看起來應該工作,但你可能想嘗試使用'String.replaceAll()'而不是'String.replace()' – phflack
你試過在替換之前和之後打印字符串'a'... – Ali786