How can i open up a file and view it
I know the following snippet does not have the functionality of opening a file for viewing.
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog( this );
if( optio开发者_开发知识库n == JFileChooser.APPROVE_OPTION ) {
try {
Scanner scanner = new Scanner( new FileReader( open.getSelectedFile().getPath() ) );
//while( scanner.hasNext() )
} catch(Exception exc) {
System.out.println(exc);
}
This snippet presents a file chooser for opening a file
In this snippet what should i do so that i am able to view the file (as i double click it)?
It could be any file in any directory
Take a look at the Desktop API: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
Desktop desktop = Desktop.getDesktop();
File file = new File("your_file.ext");
desktop.open(file);
精彩评论