1
   inputFileName = "2.txt";
   outputFileName = "3.txt";

   inputFile = new BufferedReader(new FileReader(inputFileName));
   outputFile = new PrintWriter(new FileWriter(outputFileName));

   String lineOfText = inputFile.readLine();

   while (lineOfText != null)
   {
       if (lineOfText.contains("x"))
       {
           lineOfText = lineOfText.replaceAll("x"+ ".*", "");
       } 
       outputFile.println(lineOfText);
       lineOfText = inputFile.readLine();
   } 

   inputFile.close();
   outputFile.close();

Hello, right now I have an input and output, does that mean I have two try and two catch blocks (there might be an error connecting to the previous file and writing to the second file). Or would I need only one try block?

If so, how/where would I implement the try and catch blocks?

MasterCard
  • 13
  • 1
  • 5
  • It's up to you, but it'll definitely be easier to use a single try/catch around the entire snippet. – shmosel Jan 09 '17 at 22:41
  • Ah I see. if I want to use two try blocks, how would they be implemented in this code? – MasterCard Jan 09 '17 at 22:42
  • You would need to surround any call that throws `IOException` in a try/catch block, and either return or set some boolean flag to indicate failure once you exit the block. – shmosel Jan 09 '17 at 22:45
  • I would use try-with-resources, a single block, and initiate your BR and PR within the try parenthesis. – Hovercraft Full Of Eels Jan 09 '17 at 22:47
  • I caught the exception, now when I use my main method it is telling me to throw or catch the exceptions. In each method of the main method I catch the exception, why do I still receive this message. – MasterCard Jan 09 '17 at 23:06

2 Answers2

2

I would only use one try/catch/finally-block by writing:

try {
    inputFile = new BufferedReader(new FileReader(inputFileName));
    outputFile = new PrintWriter(new FileWriter(outputFileName));
    String lineOfText = inputFile.readLine();
    while (lineOfText != null) {
        if (lineOfText.contains("x")) {
            lineOfText = lineOfText.replaceAll("x"+ ".*", "");
        } 
        outputFile.println(lineOfText);
        lineOfText = inputFile.readLine();
    } 
} catch(IOException ioe) {
        System.err.println("Caught IOException: " + ioe.getMessage());
} finally {
    if(inputFile != null)
        inputFile.close();
    if(outputFile != null)
        outputFile.close();
}

By using the finally block you can be sure that the Reader and Writer object are definitely closed.

ByteBiter
  • 116
  • 1
  • 7
  • Thank you! What does getMessage() do? – MasterCard Jan 09 '17 at 23:02
  • Basically returns the exact stacktrace of where the exception has occurred in your code. See more at [JavaDoc](https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getMessage()) – ByteBiter Jan 09 '17 at 23:10
  • 1
    `getMessage()` doesn't return the stack trace. Use `printStackTrace()` for that. Also, you should check for null in your `finally` block, in case it failed during construction. Or see @DarshanMehta's answer for a preferable approach. – shmosel Jan 09 '17 at 23:30
  • You are correct. Sorry for the misleading information. – ByteBiter Jan 09 '17 at 23:34
2

I would recommend using try with resources block of Java 7, as shown in the example below, it will take care of closing of resources as well:

public static void main(String[] args) throws Exception {
    String inputFileName = "2.txt";
    String outputFileName = "3.txt";
    try (BufferedReader inputFile = new BufferedReader(new FileReader(inputFileName));
            PrintWriter outputFile = new PrintWriter(new FileWriter(outputFileName));) {
        String lineOfText = inputFile.readLine();

        while (lineOfText != null) {
            if (lineOfText.contains("x")) {
                lineOfText = lineOfText.replaceAll("x" + ".*", "");
            }
            outputFile.println(lineOfText);
            lineOfText = inputFile.readLine();
        }
    }catch(Exception e){
        //Handle
    }
}

Here is the documentation for try with resources.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102