DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2006

    prompt for filename


    I've got a little program that will read back the contents of a dat file, however the only way I can make it work is to hardcode in the filename.

    Any suggestions what to change to make it prompt for one?

    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("filename.dat"));
    Everything I've tried so far has broken it

    Any suggestions would be great, let me know if you need me to post the rest of the code or not as well. I'm thinking it's only that line that has to be changed though.


  2. #2
    Join Date
    Aug 2003
    Is this just a console program? If it is, you can either take input from the command line or from the console during the program execution. Example:
    From Command Line:
    public static void main(String[] argv) {
      //argv[0] = first command line parameter (different from C++ style)
      ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(argv[0]));
    //.. other code here...
    Then you would invoke this with something like:
    java ProgramName "Name of file.dat"

    Alternatively, you can prompt the user for input with something like.
    public static void main(String[] argv) throws IOException {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("File Name: ");
      String fileName = in.readLine();
      ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filenName));
    // Other code here
    For this code, after you run the class, the program will pause and wait for you to enter input on the console.

    Hope this helps.

  3. #3
    Join Date
    Aug 2006
    Thank you!

    That solved it once I worked out the typo in your reply, filenName instead of fileName.

    That problem is all solved now, just need to work out the rest.

    Last edited by monkey; 08-10-2006 at 09:01 PM.

  4. #4
    Join Date
    Mar 2005
    UK, London
    I know this is solved but I though I mention you can use the joptionpane class from the swing package. Below is an example.

    import java.swing.*;

    string input = JOptionPane.showInputDialog(null,"Enter some text");

Similar Threads

  1. Help: Executing MS-Dos Prompt command by code
    By me_code2004 in forum VB Classic
    Replies: 3
    Last Post: 09-23-2005, 07:16 AM
  2. VB / ADSI Prompt User for OU.
    By ScriptRookie in forum VB Classic
    Replies: 0
    Last Post: 08-19-2005, 03:16 PM
  3. help..in vb.net with dos prompt
    By peiyoon in forum .NET
    Replies: 1
    Last Post: 10-25-2002, 01:51 PM
  4. Disable OWA Network Prompt
    By rmango in forum Enterprise
    Replies: 1
    Last Post: 12-18-2000, 10:33 AM
  5. Replies: 0
    Last Post: 03-14-2000, 12:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.