Meaningless Drivel is fun!*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Opening Files" Watch "Opening Files" New topic
Author

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
http://www.tiago182.spyw.com/
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...

importjava.io.*;

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(
ls_proc.getInputStream());

try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}

System.exit(0);
}
}

======================================
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Opening Files