Search code examples
javatextwriter

Replacing all letters and equal signs with a space


I'm trying to remove all the letters from a text file that contains random numbers and letters using this code

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

}

The problem is, it removes everything from the file, I'm very new to reading and writing in Java, so can anyone help me figure out what I'm doing wrong?


Solution

  • Here is what you're actually doing within your while loop (read the comments):

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

    This accomplishes strictly nothing.

    Here's what you want to do.

    • Assign your writer to a new file. You can always replace the original afterwards.
    • Or better, just use a StringBuilder and replace the contents of the original with the toString representation of the StringBuilder once the original reader is closed.
    • Remove the useless "characters" String array
    • Use regex as such:

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