" main"的异常错误java.util.NoSuchElementException

时间:2015-08-30 22:16:57

标签: java

我有以下两个类,我想要做的是尝试运行APP类,以便它可以采取命令行参数,而不是在代码中有一个固定的文件名

当我执行代码时,出现以下错误:

  

C:\ JavaTest> java ReadFileTestApp Resume.doc线程中的异常" main" java.util.Scanner.throwFor(未知源)的java.util.NoSuchElementException位于ReadFileTestApp.main的ReadFile.getFile(ReadFile.java:26)java.util.Scanner.next(未知源)(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());
       }

}

0 个答案:

没有答案