【问题标题】:Replacing all letters and equal signs with a space用空格替换所有字母和等号
【发布时间】:2013-10-02 20:56:37
【问题描述】:

我正在尝试使用此代码从包含随机数字和字母的文本文件中删除所有字母

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 读写非常陌生,所以谁能帮我弄清楚我做错了什么?

【问题讨论】:

标签: java text writer


【解决方案1】:

这是您在 while 循环中实际执行的操作(阅读 cmets):

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

      在您的while 循环内,在for 循环之后,您应该将该行写回文件。

      当循环进入下一行时,它会丢失str 中的所有内容。

      您已经打开了文件输出流,但从不向其中写入任何内容。它不会覆盖所有内容,它不会写入任何内容。

      【讨论】:

      • 它实际上覆盖了所有内容,基本上创建了一个新的空文件。
      • 我没有测试过,但是你能同时写回你正在阅读的文件吗?也许我是老派,但我会写临时文件,一旦完成,关闭两个文件,删除原始文件并将临时文件重命名回原来的位置
      • 如果您阅读 OP 评论中的链接,您会看到应该做的是写入临时文件并在完成后移动。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2019-03-31
      • 1970-01-01
      相关资源
      最近更新 更多