Opening a File from your computer using Java

  • PROGRAM :
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class Filr {

    public static void main(String[] args) throws IOException {
        //text file, should be opening in default text editor
        File file = new File("// enter the file path here");
        
        //first check if Desktop is supported by Platform or not
        if(!Desktop.isDesktopSupported()){
            System.out.println("Desktop is not supported");
            return;
        }
        
        Desktop desktop = Desktop.getDesktop();
        if(file.exists()) desktop.open(file);
        
        //let's try to open PDF file
        file = new File("// enter your file path here");
        if(file.exists()) desktop.open(file);
    }


}

No comments:

Post a Comment