0

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());
       }

}
Sree P
  • 9
  • 4
  • please format your post properly – Kick Buttowski Aug 30 '15 at 22:18
  • http://javarevisited.blogspot.com/2012/02/how-to-solve-javautilnosuchelementexcep.html check if it helps you – Kick Buttowski Aug 30 '15 at 22:21
  • Kick, I couldnt get much help from that.. – Sree P Aug 30 '15 at 22:24
  • can you explain this line String fileContents = new Scanner(file).useDelimiter("\\Z").next(); plz? specially this part Scanner(file)? can you post an input too? – Kick Buttowski Aug 30 '15 at 22:27
  • This uses a java.util.Scanner, telling it to delimit the input with \Z, which is the end of the string anchor. This ultimately makes the input have one actual token, which is the entire file, so it can be read with one call to next(). – Sree P Aug 30 '15 at 22:36
  • possible duplicate of [read complete file without using loop in java](http://stackoverflow.com/questions/14169661/read-complete-file-without-using-loop-in-java) – Andreas Aug 30 '15 at 22:43
  • The above code works Well if I try to open a txt file, it does open the content of the file directly in the command prompt, whereas the same doesnt execute if I try opening *.doc or any other extension file. – Sree P Aug 30 '15 at 22:46
  • I can get the output if I simply run java *.txt file. – Sree P Aug 30 '15 at 23:15
  • Here is the output C:\JavaTest>java ReadFileTestApp Temp.txt Output: Hi this is Sreenivas. – Sree P Aug 30 '15 at 23:16
  • Just a wild guess, does the input file contain a new line? If not, the `\\Z` might not match the input and the `next()` operation would fail, potentially with a NoSuchElementException... – zrvan Aug 31 '15 at 08:05

0 Answers0