-1

Ok, forgive my beginner-ness and please tell me how I can output my text from "before.txt" into a fresh new file called "after". Obviously I have altered the text along the way to make it lower-case and eliminate non alphabetic characters.

import java.io.*;

public class TextReader {

    public void openFile() throws IOException {
        try {
            // Read in the file
            BufferedReader br = new BufferedReader(
                    new FileReader(
                    new File("before.txt")));

            String currentLine = br.readLine();

            currentLine = currentLine.toLowerCase();
            currentLine = currentLine.replaceAll("[A-Z]", "");
            br.close(); // Close br to prevent resource leak
        }
        // Exception if the file is not in the path specified
        catch (Exception e) {
            System.out.println("Error: File not found");
        }
    }

    public void writeFile() throws IOException {
        BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));    
        output.write("before.txt");
        output.close();
    }
}
turbo
  • 1,887
  • 2
  • 21
  • 35

2 Answers2

1

What about this

public void openFile() throws IOException {
    try {
        // Read in the file
        BufferedReader br = new BufferedReader(
                new FileReader(
                new File("before.txt")));

        String currentLine = br.readLine();

        currentLine = currentLine.toLowerCase();
        currentLine = currentLine.replaceAll("[A-Z]", "");
        br.close(); // Close br to prevent resource leak
        writeFile(currentLine);

    }
    // Exception if the file is not in the path specified
    catch (Exception e) {
        System.out.println("Error: File not found");
    }
}

public void writeFile(String text) throws IOException {
    BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));
    output.write(text);
    output.close();
}
}

Let me guess, is this a school assignment?

cgajardo
  • 364
  • 1
  • 2
  • 17
  • I added the changes you suggested but I just get "Error: File not found". I even tried manually creating an "after" file to write to but I still get the error message. At wits end. Maybe it's my Eclipse or something? I have refreshed the explorer to see if it wrote an "after" but no joy :( – Ḟḹáḿíṅḡ Ⱬỏḿƀíé Nov 15 '13 at 20:16
  • try explicitly putting the entire file path ie( C:\file\file\before.txt) instead. also print out the actual exception (`println(e)`) so you know the exact error – vandale Nov 15 '13 at 21:47
1

Try this:

public void ReadAndWrite() throws IOException {
try {
    // Read in the file
    BufferedWriter output  = new BufferedWriter(new FileWriter("/WS3Ex3/after.txt"));
    BufferedReader br = new BufferedReader(
            new FileReader(
            new File("before.txt")));

    String currentLine;
    while((currentLine = br.readLine()) != NULL){
        currentLine = currentLine.toLowerCase();
        currentLine = currentLine.replaceAll("[A-Z]", "");
        output.write(currentLine);
    }
    br.close(); // Close br to prevent resource leak
    output.close();
}
// Exception if the file is not in the path specified
catch (Exception e) {
    System.out.println("Error: File not found");
}
}
JoeC
  • 1,850
  • 14
  • 12