Am using the following code snippet to open a file,
if (Desktop.isDesktopSupported()) {
try {
Desktop desktop = Desktop.getDesktop();
File myFile = new File(fileName);
desktop.open(myFile);
} catch (IOException ex) {}
}
if the file i try to open is of normal .txt or .pdf any such file types i can able to open the file.
But in some cases,
As shown above the Type of file is File , in this scenario the file is not opened.
if i manually open this File type file i get open with window and using a text editor i am able to open it.
How should i handle this in java? Please help..
Windows says the type of the file is "file" when there is no extension. This is why there is no default opener, and that is why Java doesn't open the file properly.
You can either rename the file and give it an extension, or use Java to directly execute the program you want to open the file with.
#open method finds the specified default app to open the specified file but in .file type there is no default app to open the file.You can only specify manually to open it in specific app.So if you set the default application that should open the .file and you have to set the default application and choose default program to open the file and than run the program.
Related
Tough question here. How do you open a file in java, in the way that when you double click on the file it automatically opens in a java application.
I'm making a musicplayer (first real big Java project for me) and I have no clue how to acheive this. When you open, lets say, a .mp3 file it will open in whatever default program you have selected for it (such as VLC mediaplayer or Windows Media Player). What I want is to be able to set the .jar file of my application as the default program for .mp3 files, and then to be able to actually launch the files in the application.
When I currently try to open a file with the application I get a windows error saying "This app cannot be executed on your pc". But when I launch the .jar itself without doing it by trying to open a .mp3 file it runs just fine.
Does anyone know how to acheive what I want? Many thanks in advance!
---edit---
I do not mean that you can select a default program for the mp3 file. The problem is that windows throws the error shown above, and that I dont know how to handle the application being launched by opening a file (which does not ope due to the error).
I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)
BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:
if (args.length > 0) {
File f = new File(args[0]);
start_the_application_with_a_file(f);
} else {
start_the_application_without_a_file();
}
C:\ProgramData\Oracle\Java\javapath\java.exe -jar "C:\Program Files\YourApp.jar" %* within a batch-file (.cmd) might do it.
I want to open a file from filechooser with a file's extension. For exapmle, if a user select the file text.doc, then the word must open this file
Desktop.getDesktop().open(File file)
This will open the file with the default program if there is a default program. If IOException occured then there is no associated application or the associated application fails to be launched.
How to close an opened excel file?
Open excel code is:
File file = new File("e:\\aaa.xlsx");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
What would be the close code?
just using java open and close Windows application like use mouse.
Forget it. Open here means that the registered application, Excel, takes over and opens a Window. After that you have no control, but watching. Excel will close.
File is a class that represents a file system path. Itself it has no state of associated reader/writer. Java 7 now parallel introduces a more evolved class Path. Besides (obviously) the path on the file system, it also stores what file system. With Path one can have more than one FileSystem, like a ZipFileSystem. That allows you to copy and rename files in a zip.
That was just an elaboration.
I need to write a java program to open a specific file extension (This is to open a .pef file). This java program is being written to open this .pef file and print it. But I don't know how to write a program that can open the .pef file extension by default.
See:
Desktop.open(File) Launches the associated application to open the file.
Desktop.edit(File) Launches the associated editor application and opens a file for editing.
Desktop.print(File) Prints a file with the native desktop printing facility, using the associated application's print command.
This presumes the application has already been associated with the file-type. To create the file association, launch your app. using Java Web Start & declare the file type in the launch file.
This solution is Windows specific
In this post I am assuming that your program is already capable of reading files of type .pef and printing them. If not then please refer to Treebranch's and Himanshu's posts here to figure out how to do so. Next I will assume that your program is to be invoked on the Windows command line as:
program.exe <filename.pef>
To programatically change the association, follow these steps:
Define a file type
ftype peffile="program.exe" "%1"
Remember to use the absolute path of program.exe here.
Associate this file type with .pef
assoc .pef=peffile
I am not familiar with this file type, but I am guessing you are going to have to read the bytes of the file and then go from there. You should be able to use a FileInputStream. Try looking at this tutorial.
You can use FileInputStream to read a file.
FileInputStream fr=new FileInputStream("xyz.pef");
int i=0;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
I want to open a PDF file from a jsp. The jsp and the PDF are in the same directory.
I am using the following piece of code:
if (Desktop.isSupported()) {
try {
File myFile = new File("<file name>.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
However, I get the error that the file is not found.
Verified user.dir and it points to my tomcat/bin.
How can I refer to the pdf to open it?
You need to specify the absolute file path. Assuming that there's a filename.pdf in the root of the public webcontent, this should do:
File myFile = new File(getServletContext().getRealPath("/filename.pdf"));
However, this construct won't work the way you'd expect. It will show the PDF file in webserver machine, not in webbrowser machine! Only when you happen to run both the webserver and webbrowser at physically the same machine, this will "work". But this does obviously not happen in real world when you publish your webapp into the internet where the webserver and webbrowser runs at physically different machines.
Instead, you just need to link to the PDF file directly.
View PDF
and let the browser handle the display.
Have you tried this? I just got this from google, so I dunno if it will work.
Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:\\Java- Interview.pdf");
p.waitFor();