2013-10-02 30 views
0

我試圖刪除包含使用此代碼更換所有的信件和等號用空格

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閱讀和編寫非常陌生,所以任何人都可以幫助我弄清楚我做錯了什麼?

+4

你覺得這行是什麼意思?'str = str.replace(str,「」);'是嗎? –

+0

您不會更換該行,也不會將更改寫入文件。 –

+0

也記住[「替換通常是不可能的文件」](http://stackoverflow.com/questions/3147615/replace-string-in-file) – Andreas

回答

1

這裏是你實際上是在做你的while循環中的內容(閱讀評論):

// iterating over the items of your own static array (without fast enumeration) 
for (int i = 0; i < symbolsAndLetters.length; i++) { 
    // initializing a new String and assigning it the value of your array 
    // that is currently indexed in your for-loop 
    String str = symbolsAndLetters[i]; 
    // replacing the String's content by replacing its value 
    // (1st parameter, the "str" value itself) 
    // with an empty String 
    str = str.replace(str, ""); 
} 

這種嚴格無所作爲。

這就是你想要做的。

  • 將作者指定給新文件。隨後可以隨時替換原件。
  • 或更好,只需使用StringBuilder,並在原始閱讀器關閉後用的 toString表示代替原始內容。
  • 這樣刪除無用的 「人物」 String陣列
  • 使用正則表達式:

    // your outer loop iterating over each line of your input file 
    
    while ((line = reader.readLine()) != null) { 
    
        // writing a new String representing the line read from the 
        // original, 
        // but replacing each of its alphabetic characters (through regex) 
        // with an empty String 
        writer.write(line.replaceAll("\\p{Alpha}", "")); 
    } 
    
0

while循環的內部,在for循環之後,應該將該行寫回文件。

當循環進入下一行時,它會丟失str中的所有內容。

您已經打開文件輸出流,但從不向其寫入任何內容。它不會覆蓋一切,它不會寫任何東西。

+3

它實際上覆蓋了一切,基本上創建一個新的空文件。 –

+0

我沒有測試過,但是你能同時寫回到你正在閱讀的文件嗎?也許我是老學校,但我會寫臨時文件,一旦完成,關閉這兩個文件,刪除原始文件並將其重新命名爲它的位置 – MadProgrammer

+0

如果您閱讀OP上的評論鏈接,看到應該完成的事情是寫入臨時文件並在完成時移動。 –

1

我會創建一個新的文件將輸出寫入。在這裏,我只是將「新」一詞添加到它。
- FileWriter writer = new FileWriter(file +「new」);

然後用正則表達式替換你想要的字符
- line = line.replaceAll(「[a-zA-Z]」,「」);

然後將它附加到新文件中:
- bwriter.append(line +「\ n」);

相關問題