0

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?

Ravekitty
  • 77
  • 1
  • 14

3 Answers3

1

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}", ""));
    }
    
Mena
  • 47,782
  • 11
  • 87
  • 106
1

I would create a new file to write output to. Here I just append the word "new" to it.
-- FileWriter writer = new FileWriter(file + "new");

Then use regular expressions to replace the characters you want
-- line=line.replaceAll("[a-zA-Z]", "");

Then append it to the new file:
-- bwriter.append(line +"\n");

Tony
  • 345
  • 1
  • 4
  • 11
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.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • 3
    It actually overwrites everything, basically creates a new empty file. – Sotirios Delimanolis Oct 02 '13 at 21:06
  • I've not tested it, but can you simultaneously write back to a file you are reading? Maybe I'm old school, but I'd be writing to temp file, once done, close both files, delete the original and rename the temp back into its place – MadProgrammer Oct 02 '13 at 21:07
  • If you read the link from the comment on the OP, you will see that what should be done is write to a temp file and move when finished. – Matt Clark Oct 02 '13 at 21:09