Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please help me with my Java code? 1. Modify the method named readLin

ID: 656492 • Letter: C

Question

Can someone please help me with my Java code?

1. Modify the method named readLineWithResources so it uses a try-with-resources statement to close the RandomAcessFile object. Run the program to make sure it works correctly. If it does, the try-with-resources statement is working correctly too.

2. Modify the method named readLineWithResources so it uses a fianlly block to close RandomAcessFile object. To do that, you'll need to delcare this object outside the try block, Add statements to the finally block that print information to the console to indicate whether the file was closed (this is typical for normal operations), never opened (which happens if the file can't be found), or unable to close (which happens only in rare cases).

3. Run the program to make sure the finally clause works as expected. To test what happens if the file is never opened, you can change the name of the file to cause a FileNotFoundExeption. To test what happens if the method doesn't work, you can throw an IOExeption just before the statement that closes the resource.

Source Code:

import java.io.*;

public class ResourcesTesterApp
{
    public static void main(String[] args)
    {
        String s1 = readLineWithResources();
        String s2 = readLineWithFinally();
    }

    public static String readLineWithResources()
    {
        System.out.println("Starting readLineWithResources method.");
       
        try
        {
            RandomAccessFile in = new RandomAccessFile("products.ran", "r");
            String s = in.readLine();
            return s;
        }
        catch (IOException e)
        {
            System.out.println(e.toString());
            return null;
        }
    }
           
    public static String readLineWithFinally()
    {
        System.out.println("Starting readLineWithFinally method.");
       
        try
        {
            RandomAccessFile in = new RandomAccessFile("products.ran", "r");
            String s = in.readLine();
            return s;
        }
        catch (IOException e)
        {
            System.out.println(e.toString());
            return null;
        }
    }   
}

Thank you!

Explanation / Answer

1).
public static String readLineWithResources() throws IOException {
System.out.println("Starting readLineWithResources method.");

try (
RandomAccessFile in = new RandomAccessFile("products.ran", "r")) {
return in.readLine();
}
}

2).
public static String readLineWithFinally() {
System.out.println("Starting readLineWithFinally method.");
RandomAccessFile in = null;
String s = null;

try {
in = new RandomAccessFile("products.ran", "r");
s = in.readLine();
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (in != null) {
try {
in.close();
System.out.println("RandomAccessFile closed");
} catch (IOException e) {
System.out.println("RandomAccessFile " + e.getMessage());
}
}
}
return s;
}