• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Opening Files

 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
nikos sokaf
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic