Opening a file from filechooser with a file's programm in Java - java

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.

Related

Java Desktop opens files with different extensions

I am using Desktop class to open files like .txt files, .pdf files, images, etc; I have a problem that when I open first file, lets say a .jpg file, the images are displayed in the image viewer, then I try to open a .pdf document, the Desktop object tries to open the .pdf file with the image viewer, and it fails.
When I open the .pdf file first, it opens correctly. After that when I try to open a .jpg file, it is opened in the pdf browser. And so it fails...
In short, the first program associated with the first file, is used to open all the other files.
how can I solve it ?

nothing shown in java applet html file chooser

When I use java applet to create a open file button, it can show a file chooser for me to choose the file, but when i access to the applet html page generated by netbean, a file chooser is shown after I clicked the open file button, but no directory is shown and I cannot select the file. What can I do to solve this problem?

Open a file using Desktop(java.awt)

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.

How to open file after user select path and save in Java?

I have a question about open csv file after save.
The situation is my program export data in .csv format and use JFileChooser to open dialog, after that user choose path and save.
Is there anyway to open exported file after save ? How can I get the path without fix it in code ?
Thank you for your help.

How close a file opened by java.awt.Desktop

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.

Categories