0

hi,
i m just wanna open/explore/view inside of any txt or doc file from folder by using JFileChooser...

here i made the code, pls tell me what need to add to open the files..

import java.io.File;
import javax.swing.JFileChooser;

public class OpenFile  {

	public static void main(String args[]) {
    
 	JFileChooser fc = new JFileChooser(".");
	
	int returnVal = fc.showOpenDialog(null);

  if (returnVal == JFileChooser.APPROVE_OPTION) {
 		File selectedFile = fc.getSelectedFile();
 			}
   
	 else if (returnVal == JFileChooser.CANCEL_OPTION) {
      System.out.println("canceled");
			}	
    }
  }
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by peter_budo
Featured Replies
  • Combine the path that JFileChooser undoubtedly returns with this: [url]http://forums.sun.com/thread.jspa?threadID=760337[/url] There might be more current solutions available, and in fact I wrote the exact program you're describing myself, but since my program is on my older machine... you're out of luck there. Read More

0

thanks for ur reply...

i just figure out with this code but i cant run any files though has no error.

would u pls check & make correction where need to edit or add ??

JFileChooser fc = new JFileChooser(".");
fc.setFileSelectionMode(JFileChooser.FILES_ONLY );
    int returnVal = fc.showOpenDialog(null);
  if (returnVal == JFileChooser.APPROVE_OPTION) {
  
        File selectedFile = fc.getSelectedFile();
        try
{
    Runtime r=Runtime.getRuntime();

String[] cmd={"cmd", "/c", "start" +fc.getSelectedFile()};
Process P=r.exec(cmd);

}
catch(IOException ioe)
{
ioe.printStackTrace();
System.exit(0);
}

Edited by doha786: n/a

0

Why on earth you want to use cmd.exe to open simple txt or doc files if you can use Apache POI?

actually i m just a beginner in Java and this code is one of the small part of my GUI that i m going to build....

any comments will be appreciated. thanks..

0

That piece of code will attempt to launch an application associated with the document formant under operating system with use of command line/prompt. In doing so you will move control from your java application to operating system. It is waste of time as this sort of things can be done through shell commands. If you wish to open/read/edit document through Java use an library like Apache POI or similar

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.