File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Opening Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Opening Files" Watch "Opening Files" New topic

Opening Files

Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Hey fellas,

I'll be very objective: I got a full path file name, and I have to open it. How to use the default operational system file loader to do it? Is there a nice way to get this done? For example, if I have a String "C:\File.pdf", how to open using the default PDF reader?

Many Tks!!!

Tiago Fernandez
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
I will post you some thoughts of me:
1)Opening a file with the proper application , on windows enviroment is a windows registry value...I don't know any good way how to set or get a windows-registry value in java code.So , This is the first problem you may have.
2)Some software opens the proper file using parametres.In that case you will need a Runtime.exec()method as follows...*;

class execInput {
public static void main(String Argv[]) {
try {
String ls_str;

Process ls_proc = Runtime.getRuntime().exec("/bin/ls -aFl");

// get its output (your input) stream

DataInputStream ls_in = new DataInputStream(

try {
while ((ls_str = ls_in.readLine()) != null) {
} catch (IOException e) {
} catch (IOException e1) {


By using this way , you will setup your Windows Explorer to open the filetypes with these parametres.

But i don't think that there is a java method that can handle your problem.If there is , please post it, it is interesting you know...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you are on Windows, Google for Java, exec and rundll32. You'll see lots of examples passing an Internet URL or a local HTML filename to start Internet Explorer. But the cool thing is that rundll32 will open the associated program for any extension. Try this: Open a DOS prompt window, type the full name of a PDF file as if it were a command. Put quotes around it in case it has spaces in the middle. If Windows has PDF mapped to Adobe or IE or some other program, it ought to pop right up.

If you have a captive audience, like a corporate office staff, you may well be able to require them to do the mapping to make the program work. If you sell it to the general public, you may not want to make your users configure their machines for you.

Hope that helps!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Opening Files
It's not a secret anymore!