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");
			}	
    }
  }

Recommended Answers

All 5 Replies

Combine the path that JFileChooser undoubtedly returns with this: http://forums.sun.com/thread.jspa?threadID=760337

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.

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);
}

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.