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 6011707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:17:55+00:00 2026-05-23T02:17:55+00:00

Most examples out there on the web for inputting a file in Java refer

  • 0

Most examples out there on the web for inputting a file in Java refer to a fixed path:

File file = new File("myfile.txt");

What about a user input file from the console? Let’s say I want the user to enter a file:

System.out.println("Enter a file to read: ");

What options do I have (using as little code as possible) to read in a user specified file for processing. Once I have the file, I can convert to string, etc… I’m thinking it has to do with BufferedReader, Scanner, FileInputStream, DataInputStream, etc… I’m just not sure how to use these in conjunction to get the most efficient method.

I am a beginner, so I might well be missing something easy. But I have been messing with this for a while now to no avail.

Thanks in advance.

  • 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-05-23T02:17:55+00:00Added an answer on May 23, 2026 at 2:17 am

    To have the user enter a file name, there are several possibilities:

    As a command line argument.

    public static void main(String[] args) {
      if (0 < args.length) {
        String filename = args[0];
        File file = new File(filename);
      }
    }
    

    By asking the user to type it in:

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a file name: ");
    System.out.flush();
    String filename = scanner.nextLine();
    File file = new File(filename);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most examples of creating remote branches involve pushing from a local branch Is there
In most examples that you find on the web when explicitly not using using,
I am confused about authentication with BlazeDS. Most of the few examples I have
I've seen a lot of examples on the web where forms are laid out
I understand there are a variety of ways to design web applications using java-ee,
Most examples show how to redirect all subdomain traffic to a primary domain, maintaining
It seems like most examples of JPA/Hibernate entity bean classes I've seen do no
I just wondered, why most Delphi examples use FillChar() to initialize records. type TFoo
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
What are the examples where we really need 3 Tier Architecture ? Can Most

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.