Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Creating a file - What is wrong here?(Or How to Access File System Resources?)
Creating a file - What is wrong here? [message #685739] Sat, 18 June 2011 17:54 Go to next message
Eclipse UserFriend
Hi,

I am trying to understand what the problem is with accessing a file system resource. I did quite a bit of Googling, but couldn't find anything on this.

Below is the code I am experimenting with. It always throws a FNFE, also when I change it to input stream and point it to an existing file.

I am clearly missing something, but what?

   URL url = FileLocator.toFileURL(Platform.getBundle(""my.bundle.id).getEntry("./../.."));
    String urlString = url.toString();
    IPath urlPath = new Path(urlString).append("myfile.txt");
    File file = urlPath.toFile();
    file.createNewFile();   <----- FileNotFoundException
//    InputStream fis = new FileInputStream(file);  <-------- FNFE
    OutputStream os = new FileOutputStream(file);
    os.write("Hello".getBytes());
    os.flush();
    os.close();
} catch (IOException e) {
	LOG.error(e.getClass() + ": " + e.getMessage());
}

Your help is highly appreciated.

Kind Regards,

Erwin
Re: Creating a file - What is wrong here? [message #685740 is a reply to message #685739] Sat, 18 June 2011 18:29 Go to previous messageGo to next message
Eclipse UserFriend
You are barking up the wrong tree. If you want to access a resource in the workspace, look at IResource and IFile.

Start with IWorkspaceRoot root = ResourcesPlugin.getWorkspaceRoot();

I thought there was a paper on this, but if you look for uses of IFile then you will see how you can address resources, create them, get their contents etc.

If I got it wrong, post again to clarify your question.
icon14.gif  Re: Creating a file - What is wrong here? [message #685744 is a reply to message #685740] Sat, 18 June 2011 21:12 Go to previous message
Eclipse UserFriend
Thanks for your help Francis,

That pointed me in the right direction. With your help I found this: "FAQ How do I open an editor on a file outside the workspace?" (cannot use links yet).

After a little more tinkering this, is what I was looking for:

        IWorkspace ws = ResourcesPlugin.getWorkspace();
	IProject project = ws.getRoot().getProject("Arbitrary Project Name");
	if (!project.exists())
		// project NEVER exists ???
	    project.create(null);
	if (!project.isOpen())
	    project.open(null);
	IPath location = new Path("/Users/erwin/work/crazyPath/myfile.txt");
	IFile file = project.getFile(location.lastSegment());
	file.createLink(location, IResource.NONE, null);
	InputStream fis = file.getContents();


Regards,

Erwin
Previous Topic:Common Navigator In RCP
Next Topic:Strange behavior on setSaveAndRestore(true).
Goto Forum:
  


Current Time: Sat Aug 30 14:35:42 EDT 2025

Powered by FUDForum. Page generated in 0.17384 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top