Tutorial

How to open a File in Java

Published on August 4, 2022
author

Pankaj

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(s)

Category:
Tutorial
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?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
August 17, 2013

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

- mike

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 13, 2014

    Amazing tutorial and straight to the point.

    - Kon

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 31, 2015

      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

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        January 14, 2016

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

        - Shalini J

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          August 29, 2016

          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

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 19, 2018

          Perhaps the URI contains blank spaces.

          - Manolo

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 23, 2016

            It’s working fine. Thank you.

            - Ritesh

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              January 8, 2017

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

              - AYENI FEMI

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                March 24, 2017

                appreciate it man … thank you …

                - iffe

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  April 22, 2017

                  Thanks for the code.It is very helpful for me

                  - Ramu

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    July 15, 2018

                    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

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      July 28, 2018

                      opening the files , this way --> pop up is coming with open file and ask to open in read only mode.because this is locked by some user.

                      - manjay.kumar

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        July 28, 2018

                        This always ask to open file in read mode and locked by some one.

                        - Ganesh

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          January 25, 2019

                          Thank you very much your small piece of code make my life more flexible

                          - Mali

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            March 13, 2019

                            While opening Word document using above code template , my document saying This document contains links that may refer to other files do you want to update this document with data from the linked files ? set as true with java… So how can handle that warning/alert popup using java ? I need that warning/alert popup and need to handle through programming . plz give me reply with solution as early as possible , thank you

                            - kesav

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              March 22, 2019

                              Can anyone tell me How to open any file based on user search from D drive? can anyone code for it?

                              - nadim kazi

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                December 9, 2019

                                I am not able to read/Write word file from a shard network.What to change from the above code read/Write from a shared network. Thanks- Dinesh

                                - Dinesh

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  July 19, 2020

                                  how to excute a java file which is downloaded in runtime, then copied as new java file. the new java file will be complied under same package of the running application. I used threads join, wait. did not work. note new file was not present when application launched. it is later download. so i got file not exception. how to handle until download and copy is done. then excute it

                                  - monsur

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    October 29, 2020

                                    Please help. “desktop is not support” msg is coming for me. I am using windows.

                                    - Mansi Sharma

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    October 29, 2020

                                    and i m using jdk 13.0.2

                                    - mansi sharma

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      February 4, 2021

                                      Does it have to be a java file to open? Say I wanted to open Widows Mail (Microsoft email application). Would that work?

                                      - Jeffrey

                                        Try DigitalOcean for free

                                        Click below to sign up and get $200 of credit to try our products over 60 days!

                                        Sign up

                                        Join the Tech Talk
                                        Success! Thank you! Please check your email for further details.

                                        Please complete your information!

                                        Become a contributor for community

                                        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                        DigitalOcean Documentation

                                        Full documentation for every DigitalOcean product.

                                        Resources for startups and SMBs

                                        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                        Get our newsletter

                                        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                        New accounts only. By submitting your email you agree to our Privacy Policy

                                        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.