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

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 6 Years Ago by doha786: n/a

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

This question has already been answered. Start a new discussion instead.