Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Opening a File for User

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Opening a File for User

    Quick question.

    I'm writing a program that reads/writes Excel files and whatnot. When the program finishes, I will prompt the user, asking if they would like the Excel File to be opened for them.

    How can I open the file for the user?

    I know it's possible, but I cant get anything from google b/c everything I have tried searching just gives results concerning reading/writing files instead of actually opening them.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Opening a File for User

    Do you mean open the file in excel? You can make a call to System.exec, or alternatively use the new Desktop (Java Platform SE 6) class and call Desktop.open (you should make sure the file has the appropriate extension so that it is recognizes as an excel file)

  3. The Following User Says Thank You to copeg For This Useful Post:

    aussiemcgr (July 28th, 2010)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Opening a File for User

    Fantastic. Exactly what I needed. Thanks copeg.

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Opening a File for User

    You could do something like this.

    It's just an example but a JOptionPane will popup with YES and NO. If you click YES, the file will open.

    		String fileName = "file.txt";
     
    		// File written. Open?
    		JOptionPane pane = new JOptionPane("file saved. Open file?");
    		Object[] options = new String[] { "YES", "NO" };
    		pane.setOptions(options);
    		JDialog dialog = pane.createDialog(new JFrame(), "File Saved");
    		dialog.show();
    		Object obj = pane.getValue();
     
    		int result = -1;
    		for (int k = 0; k < options.length; k++)
    			if (options[k].equals(obj)){
    				result = k;
    				String opt = Integer.toString(result);
    				// if YES
    				if(opt.equals("0")){
    					// Open file
    					Process p = Runtime.getRuntime().exec("cmd /c \""+fileName+"\"");			
    				}else{
    					// do nothing
    				}
    			}

    This opens the file:

    Process p = Runtime.getRuntime().exec("cmd /c \""+fileName+"\"");

    You will need to import:

    javax.swing.JOptionPane;
    javax.swing.JDialog;
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM
  2. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM
  3. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM
  4. Libraries for User Tracking
    By jwpa in forum The Cafe
    Replies: 4
    Last Post: August 4th, 2009, 09:32 AM
  5. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM