I have these below two classes and what I am trying to do is trying to run the APP class so it can take command line arguments instead of having a fixed file name in the code
When I execute the code, I get the following errors:
C:\JavaTest>java ReadFileTestApp Resume.doc Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at ReadFile.getFile(ReadFile.java:26) at ReadFileTestApp.main(ReadFileTestApp.java:8)
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class ReadFile {
private String fileName = "";
private long maxSize = 102400;
ReadFile(){};
ReadFile(String fileName, Long maxSize) {
this.fileName = fileName;
this.maxSize = maxSize;
}
public String getFile() throws FileNotFoundException {
File file = new File(this.fileName);
if (file.exists()) {
double fileSize = file.length();
if (fileSize > this.maxSize) {
return "File is larger than max size";
} else if (fileSize == 0) {
return "File is empty";
} else {
String fileContents = new Scanner(file).useDelimiter("\\Z").next();
return fileContents;
}
}else {
return "File not Found!";
}
}
}
import java.io.FileNotFoundException;
public class ReadFileTestApp extends ReadFile
{
public static void main(String [] args) throws FileNotFoundException {
ReadFile rf = new ReadFile(args[0], (long) 102400);
System.out.println(rf.getFile());
}
}