Question: I need to take this code: public class Example { /** * @param args the command line arguments */ public static void main(String[] args) {

I need to take this code:

public class Example { /** * @param args the command line arguments */ public static void main(String[] args) { // Read the filename from the command line argument String filename = args[0]; BufferedReader inputStream = null; String fileLine; try { inputStream = new BufferedReader(new FileReader(filename)); System.out.println("Email Addresses:"); // Read one Line using BufferedReader while ((fileLine = inputStream.readLine()) != null) { System.out.println(fileLine); } } catch (IOException io) { System.out.println("File IO exception" + io.getMessage()); } finally { // Need another catch for closing // the streams try { if (inputStream != null) { inputStream.close(); } } catch (IOException io) { System.out.println("Issue closing the Files" + io.getMessage()); } } } }

And add this method to the code:

class ReadNames { // ... Other methods and variables public static final int fileSizeLimit = 1000000; public ReadNames(String filename) throws IOException { long size = Files.size( Paths.get( filename)); if (size > fileSizeLimit) { throw new IOException("File too large"); } else if (size == 0L) { throw new IOException("File size cannot be determined, possibly too large"); } this.input = new FileReader(filename); this.reader = new BufferedReader(input); } } I don't know how to add the two parts of the code together.

The above code imposes a limit on the size of the file being read. The limit is set with the Files.size() method, which was introduced in Java SE 7.

Can someone help me?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!