• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Casting FileObject to File

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Am using VFSFileChooser to open files on my remote machine. This chooser returns a FileObject. I have a method which reads only files of type File. I tried to cast the FileObject to File but its returning Exception.

How can i convert FileObject to File???

-------------------------------
2009.32.09 05:32:07 Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.apache.commons.vfs.provider.sftp.SftpFileObject
at gov.sandia.ccaffeine.dc.user_iface.gui.ActionsPanel.doOpenAction(ActionsPanel.java:357)
at gov.sandia.ccaffeine.dc.user_iface.gui.Builder.openPrewiredBldMenuItemActionPerformed(Builder.java:1165)
at gov.sandia.ccaffeine.dc.user_iface.gui.Builder.access$26(Builder.java:1162)
at gov.sandia.ccaffeine.dc.user_iface.gui.Builder$27.actionPerformed(Builder.java:850)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1882)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2202)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:334)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1051)
at apple.laf.CUIAquaMenuItem.doClick(CUIAquaMenuItem.java:119)
at javax.s
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the inheritance trees of all known FileObject implementations and File have only one thing in common: java.lang.Object is the root.

I've shown in this thread how to convert a File into a FileObject. The other way around is harder; the getLocalFile() method of class LocalFile (which is the only FileObject implementation you could ever convert to a File - File is essentially for the local file system only) is abstract.

You can try using getPath, but that will return the absolute path within the FileSystem. You could try through getFileSystem().getRootName() to retrieve the absolute path of the root (on the native file system) but I'm not sure if that will work.
Once you have the full absolute path, you can of course simply call new File(absolutePath).
 
Jiss Elizabeth
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You!

I was able to get the inputstream,

FileContent fc = fileObject.getContent();
InputStream is = fc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic