prompt for filename


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: prompt for filename

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Posts
    3

    prompt for filename

    Hi,

    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?

    Code:
    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.

    Thanks

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    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:
    Code:
    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.
    Code:
    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.
    ~evlich

  3. #3
    Join Date
    Aug 2006
    Posts
    3
    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.

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

  4. #4
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    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, 08:16 AM
  2. VB / ADSI Prompt User for OU.
    By ScriptRookie in forum VB Classic
    Replies: 0
    Last Post: 08-19-2005, 04:16 PM
  3. help..in vb.net with dos prompt
    By peiyoon in forum .NET
    Replies: 1
    Last Post: 10-25-2002, 02:51 PM
  4. Disable OWA Network Prompt
    By rmango in forum Enterprise
    Replies: 1
    Last Post: 12-18-2000, 11:33 AM
  5. Replies: 0
    Last Post: 03-14-2000, 01: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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