Re: Is It Possible to Make Java Find a File on Your Computer?
Lew wrote:
They added some really nice features to version 6. The Desktop class
being one of my favorites. Runtime.exec() has a bunch of issues. I
[quoted text clipped - 6 lines]
instance of MS Excel is opened in a new Eclipse editor window!
I don't know if this capability was present in 3.2, but it sure is in 3.3.
Would this work to bring up Open Office if I don't have Excel on my machine?
..hmmm. Yes, no, maybe..
I was playing with this code..
<code>
import java.awt.*;
import java.io.*;
class FileTypeHelper {
public static boolean openFile(File document) throws Exception {
Desktop dt = Desktop.getDesktop();
try {
dt.open( document );
return true;
} catch (UnsupportedOperationException ex) {
return false;
}
}
public static void main(String[] args) {
File f = new File("Site.xls");
try {
System.out.println(openFile(f));
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
</code>
..and can report the following.
This PC is running 1.6, Win XP Pro, no Excel
but OpenOffice..
When that code runs, and "Site.xls" exists in the
current directory, it will open the file in OpenOffice
as an *editable* file, which confused me since there
is also the method Desktop.edit(File), which is
intended to open a file for edit (maybe OO has no
concept of uneditable* files?). In any case, when
I change the above to 'edit' - it fails and throws an
'Unsupported' exception.
* Beyond what is flagged on the file itself.
I was trying to include some data in my post, to
turn the code above into an SSCCE (I could probably
load a smallish XLS to my site if anybody else is
interested in running their own tests to see the
results) so tried created a CSV file and 'claiming'
(by the file extension) that it was .XLS.
When I specified 'open' for that '.XLS', OpenOffice
was invoked, but it dropped the content into the
'Writer' (text editor), rather than 'Calc' (spreadsheet).
After I'd changed the name to .csv, the method opened
it in ..Notepad. :-(
Here is the text used in my simple (3 field, 3 line) CSV..
****************
"January",43.2,"Midge Green"
"February",48.9,"John Smiley"
"March",45.6,"Minh Truong"
****************
(As an aside - I would be looking to use the Desktop
methods before going off to try and find anything, the
strategy outlined by Manish and myself early in the
thread should probably be considered an 'if all else
fails' fall-back strategy. I am curious to check that
the simple 'open' command will end in an *editable*
file in *Excel*.)
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1