#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0

    File reader based on user input


    Yesterday I was playing around with file readers, trying to get things to work. I managed to get things working but I have a question. It was only possible to read from a file when I specified what the file name was in the code. What I'd like to do is prompt the user for the file name to output.

    How do I do this?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Look at the JFileChooser class. It presents a prompt that the user can fill in.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    18
    Rep Power
    0
    Originally Posted by NormR
    Look at the JFileChooser class. It presents a prompt that the user can fill in.
    I actually was experimenting and did it a different way. I need to stop creating threads before trying on my own.

    This is what I did.
    Code:
    System.out.println("Please input the name of the file");
    Scanner file = new Scanner (System.in);
    String filename = file.next();
    Scanner in = new Scanner(new FileReader(filename));
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Having the user enter some text that the code uses as a path to a file can be a problem if the user doesn't spell the path correctly.
    Reading user input into a String is a lot easier for you to code.

IMN logo majestic logo threadwatch logo seochat tools logo