0

I have a method called readinFile and if the user enters a wrong file instead of exiting I wanted to call the method readinFile again inside the readinFile method I ask the user for new filename. The problem I am running into is the first time it goes through it and gives the exception file not found than it goes through the catch(). I want it to call the method and not run the last inputStream.

try 
{
    inputStream = new Scanner(new FileInputStream(fileName));
}
catch(FileNotFoundException E)
{
    readinfile(table, numberOfColumns, header,
               original, sntypes,displaySize, 
               writeOut,inputStream,fileName );
    System.out.print("It got here after doing the method call");        
}
Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
Ashley
  • 55
  • 7
  • I don't think it's a nice design, but the small amount of code you've shown sounds like it should do what you want. It's not clear what's going on - please provide more code. – Jon Skeet Sep 05 '11 at 17:31
  • 2
    I read your question 4 times and didn't understand what your problem is – Bozho Sep 05 '11 at 17:31
  • it will read the file than it goes back to where it was before the method call. – Ashley Sep 05 '11 at 17:34
  • 'if (!lookFile.exists()) { System.out.println("*** Error *** \n" + "Your text file has the wrong name or is " + "in the wrong directory. \n" + "Aborting program...\n\n"); readinfile(table, numberOfColumns, header, original, sntypes,displaySize, writeOut, inputStream ); // System.exit(-1); // Terminate the program } ' – Ashley Sep 05 '11 at 17:49

5 Answers5

2

You should generally not use exceptions for branching. Just check for the existance of the file using File.exists, like so:

new File(fileName).exists()
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
  • 1
    Just note that between the check for existence and the actual use of the file (suppose it should be read) the file system could've changed, and the file could have been removed/renamed/modified. Therefore, when you will actually read the file, unless you have other reasons, you cannot assume that it still exists, and the `FNFE` might get thrown! – Bruno Reis Sep 05 '11 at 17:36
  • Yeah, but that is a very, very rare Scenerio which then really would warrant an Exception. Whereas the non-existance of a File after user input is normal flow. – Janick Bernet Sep 05 '11 at 17:42
2

You probably want to do something like this:

String fileName;

do {
    System.out.println("Please enter filename");
    fileName = getFileNameFromInput();
    File file = new File(fileName);
} while (!file.exists());

readFile(file);

EDIT:

As Bruno Reis has pointed out, this will only check if the file exists when the user specified the file name. If the file was to be moved/deleted between specifying the file name and reading it then a FileNotFoundException would still be thrown. To reduce the risk of this you can lock the file as discussed in this question.

Community
  • 1
  • 1
luketorjussen
  • 3,156
  • 1
  • 21
  • 38
  • The same as below: when the code in `readFile` gets executed, the file could have been deleted (even if it existed on the `file.exists()` check. The code in `readFile` should be ready to deal with a `FNFException` (unless, for some reason, you can guarantee that once the file exists, it cannot be removed, for example) – Bruno Reis Sep 05 '11 at 17:39
  • In which case his original try catch is probably the most simple solution – luketorjussen Sep 05 '11 at 17:40
0
bool invalidFilename = true;
string fileName;

while(invalidFilename)
{
    readinfile(...);   
    invalidFilename = !new File(fileName).exists();
}

inputStream = new Scanner(new FileInputStream(fileName));
Matten
  • 17,365
  • 2
  • 42
  • 64
0

You can check if the filename the user input does exists or not, and don't need to catch the exception. (which is not a good design code, decrease the readability of the code)....

as inflagranti said,

you can do this pseudocode

if (!new File(filename).exists()){
    //read your other file from user
    readinfile(....)

}
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
0

To get what you are after, without the chance of the file being deleted after you check for it existing but before you open it do something like:

boolean done = false;
String fileName = fileNameParameter;

while(!done)
{
    try 
    {
        inputStream = new Scanner(new FileInputStream(fileName));
        done = true;
    }
    catch(FileNotFoundException E)
    {
        fileName = /* ask the user for the file name */
    }
}
TofuBeer
  • 60,850
  • 18
  • 118
  • 163