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

3 回答 3

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, ""); 
}

这完全没有任何作用。

这就是你想要做的。

  • 将您的作家分配给一个新文件。之后您可以随时更换原件。
  • 或者更好的是,只需使用 a并将原始阅读器关闭后StringBuilder的内容替换为原始阅读器的 toString表示即可。StringBuilder
  • 删除无用的“字符”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}", ""));
    }
    
于 2013-10-02T21:23:05.017 回答
1

我会创建一个新文件来写入输出。在这里,我只是将“新”这个词附加到它上面。
-- FileWriter writer = new FileWriter(file + "new");

然后用正则表达式替换你想要的字符
——line=line.replaceAll("[a-zA-Z]", "");

然后将其附加到新文件中:
-- bwriter.append(line +"\n");

于 2013-10-02T21:39:27.703 回答
0

Inside of your while loop, after the for loop, you should be writing the line back to the file.

When the loop steps to the next line, it looses everything in str.

You already open the file output stream, but never write anything to it. It does not overwrite everything, it does not write anything.

于 2013-10-02T21:03:31.947 回答