我試圖刪除包含使用此代碼更換所有的信件和等號用空格
package textEdit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class RemoveLetters {
public static void readFile(String file) {
try {
FileReader fis = new FileReader(file);
BufferedReader reader = new BufferedReader(fis);
FileWriter writer = new FileWriter(file);
BufferedWriter bwriter = new BufferedWriter(writer);
String line = null;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < symbolsAndLetters.length; i++) {
String str = symbolsAndLetters[i];
str = str.replace(str, "");
}
}
reader.close();
bwriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String[] symbolsAndLetters = {"A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
"Z", "=","a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z",};
/**
* @param args
*/
public static void main(String[] args) {
readFile("c:/users/Kyle/workspace/Learn more java/src/music.txt");
}
}
的問題是,它從消除一切隨機數字和字母文本文件中的所有字母文件,我對Java閱讀和編寫非常陌生,所以任何人都可以幫助我弄清楚我做錯了什麼?
你覺得這行是什麼意思?'str = str.replace(str,「」);'是嗎? –
您不會更換該行,也不會將更改寫入文件。 –
也記住[「替換通常是不可能的文件」](http://stackoverflow.com/questions/3147615/replace-string-in-file) – Andreas