Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:50:43+00:00 2026-06-14T19:50:43+00:00

EDIT: The working code: Scanner input = new Scanner(System.in); String inputFile = Computer_Store_DSV.txt; String

  • 0

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”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:50:44+00:00Added an answer on June 14, 2026 at 7:50 pm

    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()).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scanner scanner = new Scanner(system.in); String input = new String(); while(!input.equalsIgnoreCase(stop)) { input =
i'm working with TFS and i need to edit file localy without checking it
Edit: Turns out I just had some values -switched-. I'm working with OpenGL and
EDIT: See end of my post for working code, obtained from zeekay here .
EDIT: To answer some questions, this is the revised and still not working code
Edit: Below is my working code based on the feedback/answers I recieved. This question
Final EDIT working code (Huge thanks to Claus for taking time and solving it):
I need a working code for a function that will return a random string
I won't want to have edit any working sets. I just want a way
im using the WPFToolkit's DataGrid and im trying to get an edit button working,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.