Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using File IO declared in a loop

EDIT:

The working code:

    Scanner input = new Scanner(System.in);
    String inputFile = "Computer_Store_DSV.txt";
    String userInputFile;

    File sourceFile;
    do {
        System.out.println("Please enter the path of the file, enter to use the default path");
        userInputFile = input.nextLine();
        if (!"".equals(userInputFile)) {
           inputFile = userInputFile;   
        }
        sourceFile = new File(inputFile);

        System.out.println("Path is = " + inputFile);

        if (!sourceFile.canRead()) {
            System.out.println("Unable to read the file");
        }
    } while (!sourceFile.canRead());
    Scanner record = new Scanner(sourceFile);

Thanks breath for your help.

I have the following code:

Scanner input = new Scanner(System.in);
boolean fileLoop = false;
String inputFile = "Computer_Store_DSV.txt";
String userInputFile;

    do {
        System.out.println("Please enter the path of the file, enter to use the default path");
        userInputFile = input.nextLine();
        if (!"".equals(userInputFile)) {
            inputFile = userInputFile;
        }
        File sourceFile = new File(inputFile);
        System.out.println("Path is = " + inputFile);

        if (!sourceFile.canRead()) {
            System.out.println("Unable to read the file");
            fileLoop = true;
        }
    while (fileLoop);
    Scanner record = new Scanner(sourceFile);

As you can see I am trying to "validate" if a file exist before starting the mail program.

Is there a way to do it without using the File class twice "for validating and for the actual program"?

like image 743
Mohammed J. Sharaf Avatar asked Apr 21 '26 21:04

Mohammed J. Sharaf


1 Answers

Please take at look at this solution. I believe it does what you want to achieve. I have restructured the code and used 2 additional methods. In the end i am closing the input because you can create a memory leak in case it remains open.

I have tested the code for any file location and it retrieves it without any problem. You can retrieve a file from within the project or any location of your computer. The only thing you need to provide is the path (without the name of the file) because as long as you are using always the same file you can take advantage of the concat() method of String.

Assume that the file is located in the directory /foo/fooBar/Examples/Computer_Store_DSV.txt. In case you want to access the file you should write foo/fooBar/Examples/ (be careful of the last slash: *YOU MUST PROVIDE IT*). Another example: If the file is located inside the root folder of your project by pressing enter it will find right way. If it is inside your project in a folder named resources then you just have to write resources/ and it will find the file.

In case the file is found, it informs you that the file was found, it makes the fileLoop false and creates a Scanner named record.

You will see that the methods are declared as static. They are like this because i am using them inside the main and they must be static. If your code is not located inside a main class then you can remove the static from the method.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestScanner {
    public static void main(String args[]) throws FileNotFoundException {
        Scanner input = new Scanner(System.in);
        boolean fileLoop = true;
        String inputFile = "Computer_Store_DSV.txt";
        String userInputFile;
        File sourceFile = null;
        Scanner record = null;
        do {
            System.out.println("Please enter the path of the file, or press enter to use the default path");
            userInputFile = input.nextLine();

            if (!userInputFile.isEmpty() && new File(userInputFile.concat(inputFile)).exists() ) {
                inputFile=userInputFile.concat(inputFile);
                printState(inputFile,"File found.!");
                sourceFile = new File(inputFile).getAbsoluteFile();
                fileLoop=readFile(sourceFile);
                record = new Scanner(sourceFile);
            }
            else if (new File(inputFile).getAbsoluteFile().exists()){
                sourceFile = new File(inputFile).getAbsoluteFile();
                printState(inputFile,"File found.!");
                fileLoop=readFile(sourceFile);
                record = new Scanner(sourceFile);
            }
            else{
                fileLoop=true;
                printState(inputFile,"File does not exist.!");
            }
        } while (fileLoop);
        input.close();

    }

    public static boolean readFile(File sourceFile){
        if (!sourceFile.canRead()) {
            System.out.println("Unable to read the file");
            return true;
        }
        else
            return false;
    }

    public static void printState(String inputFile,String state){
        System.out.println(state);
        System.out.println("Path used:  " + new File(inputFile).getAbsolutePath()+"\n");
    }
}

You can also avoid the creation of the sourceFile by passing the following

new File(inputFile).getAbsoluteFile()

directly to the Scanner like this

record = new Scanner(new File(inputFile).getAbsoluteFile()).
like image 52
Konstantinos Margaritis Avatar answered Apr 23 '26 11:04

Konstantinos Margaritis