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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started