This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting FileObject to File  RSS feed

 
Jiss Elizabeth
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!