Report this

What is the reason for this report?

How to open a File in Java

Published on August 4, 2022
How to open a File in Java

Sometimes we have to open a file in java program. java.awt.Desktop can be used to open a file in java. Desktop implementation is platform dependent, so first, we should check if the operating system supports Desktop or not. This class looks for the associated application registered to the current platform to open a file.

Java Open File

java open file, how to open a file in java Let’s have a look at the simple java open file program. If we try to open a file that doesn’t exist, it will throw java.lang.IllegalArgumentException. Let’s see Desktop class example for java open file. JavaOpenFile.java

package com.journaldev.files;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class JavaOpenFile {

    public static void main(String[] args) throws IOException {
        //text file, should be opening in default text editor
        File file = new File("/Users/pankaj/source.txt");
        
        //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("/Users/pankaj/java.pdf");
        if(file.exists()) desktop.open(file);
    }

}

When you run the above program, the text file will be opened in the default text editor. Similarly, a PDF file will be opened in adobe acrobat reader. If there are no application associated with given file type or the application is failed to launch, open method throws java.io.IOException. That’s all for a simple program to open a file in java.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

Awesome! Easy to read your code and straight to the point without all the extra fluff. Bookmarking your blog. Thanks for the help!

- mike

Amazing tutorial and straight to the point.

- Kon

I am developing web project in java.I have requirement to open a pdf file in jsp.That pdf is available in web directory.Please help me

- Amaranath

Thanks for the code above, it helped me in solving my problem in opening an excel from Java code.

- Shalini J

I am trying to access a file shared in my network. It shows : java.net.URISyntaxException Can someone help me to solve this problem?

- Sidiki Traore

It’s working fine. Thank you.

- Ritesh

THANKS FOR THE STRAIGHT TO THE POINT AND STATE-OF-THE ART SOLUTIONS TO PROBLEM THUMB UP BRUV!!!

- AYENI FEMI

appreciate it man … thank you …

- iffe

Thanks for the code.It is very helpful for me

- Ramu

Always valid the dependencies before continue. Example, Desktop.isDesktopSupported() can fail, therefore instead of open file first unnecessarily, in advance you should valid all crucial conditions to your program.

- dio

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.