Open a File in Java - 3 Effective Ways to Achieve

Being the platform for developing almost anything that you dream of, opening a file in Java is just a tiny thing infront of it and you can easily achieve this using the Runtime, ProcessBuilder and the Desktop classes. These are the 3 effective ways that i can suggest for you to open external files in Java.


import java.awt.*;
import java.io.*;
class OpenAFile
{
public static void main(String args[]) throws Exception
{

// First way. Using the java.lang.Runtime class

// Create a Runtime object
Runtime r=Runtime.getRuntime();
// Open a file
r.exec("notepad.exe OpenAFile.java");

// Second way. Using the java.lang.ProcessBuilder class


// Create a ProcessBuilder object
ProcessBuilder pb=new ProcessBuilder("notepad","OpenAFile.java");

// Start opening the file
pb.start();

// Third way. Using the java.awt.Desktop class

// Get a Desktop object
Desktop dk=Desktop.getDesktop();

// Open a file
dk.open(new File("OpenAFile.java"));

}
}



Runtime class: Runtime class is in the java.lang package. This class contains the exec(String file) method which is capable of executing (not opening) a file i.e. it can open applications instead of normal files that do depend on other applications to be opened. So, in order to open a normal file using this, we can specify the command line parameters as shown in the above example. This is simple, all you need to do is to first specify the, path and the file name of the application that you are going to open a file with followed by a space and then the complete path and the name of the file.  Here in the above example, i opened the same program which is in the same directory, the notepad too as is a system pre-built application it can easily be accessed with its name and there is no need to specify it's complete path as it (the path) was already specified in the Environment Variables of Windows Operating System. If it is a new application, then you must place the entire path of the application program followed by a space and then the path of the file that you are going to open.

r.exec("notepad.exe OpenAFile.java"): The method takes a string which points the path of the program that is to be executed. Here, i want to execute notepad.exe and with this application i want to open OpenAFile.java so i specified it as an argument for the application.

ProcessBuilder class: Unlike the Runtime class, ProcessBuilder class is quite efficient because the method in it takes 2 parameters (here), the first one is the application and the next one is the file that is to be opened with the specified application.

Desktop: The last class, is the most efficient of all. If your program is to execute in an entirely new system, and don't know what applications it has or what applications can open a file. You can use java.awt.Desktop class to open a file.

Desktop.getDesktop(): This returns an object of the Desktop class.

dk.open(new File("OpenAFile.java")): This method takes a file which takes a string that points a normal file (not an application) and opens it with the default application associated with that file type. Looks more efficient! :)

throws Exception: As we are using a File in the open method discussed above, the method throws an IOException if the file may not be granted for reading or opening purpose.


Posted by at on

Tags: Abstract Window Toolkit, Java IO, java.lang, Swing Hacks,