Difference between revisions of "FAQ How are resources created?"

From Eclipsepedia

Jump to: navigation, search
 
 
(One intermediate revision by one user not shown)
Line 6: Line 6:
 
create the resource.  The following snippet uses resource handles to create a project, a folder, and
 
create the resource.  The following snippet uses resource handles to create a project, a folder, and
 
a file.
 
a file.
<pre>
+
 
  IWorkspace workspace = ResourcesPlugin.getWorkspace();
+
<source lang="java">
  IWorkspaceRoot root = workspace.getRoot();
+
IWorkspace workspace = ResourcesPlugin.getWorkspace();
  IProject project  = root.getProject("MyProject");
+
IWorkspaceRoot root = workspace.getRoot();
  IFolder folder = project.getFolder("Folder1");
+
IProject project  = root.getProject("MyProject");
  IFile file = folder.getFile("hello.txt");
+
IFolder folder = project.getFolder("Folder1");
  //at this point, no resources have been created
+
IFile file = folder.getFile("hello.txt");
  if (!project.exists()) project.create(null);
+
//at this point, no resources have been created
  if (!project.isOpen()) project.open(null);
+
if (!project.exists()) project.create(null);
  if (!folder.exists())  
+
if (!project.isOpen()) project.open(null);
      folder.create(IResource.NONE, true, null);
+
if (!folder.exists())  
  if (!file.exists()) {
+
    folder.create(IResource.NONE, true, null);
      byte[] bytes = "File contents".getBytes();
+
if (!file.exists()) {
      InputStream source = new ByteArrayInputStream(bytes);
+
    byte[] bytes = "File contents".getBytes();
      file.create(source, IResource.NONE, null);
+
    InputStream source = new ByteArrayInputStream(bytes);
  }
+
    file.create(source, IResource.NONE, null);
</pre>
+
}
 +
</source>
  
 
This example defensively checks that the resource doesn&#146;t already exist before trying
 
This example defensively checks that the resource doesn&#146;t already exist before trying
Line 31: Line 32:
 
a progress monitor in a real application.
 
a progress monitor in a real application.
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
{{Template:FAQ_Tagline}}

Latest revision as of 13:55, 30 May 2013

The workspace is manipulated using resource handles. Resource handles are lightweight pointers to a particular project, folder, or file in the workspace. You can create a resource handle without creating a resource, and resources can exist regardless of whether any handles exist that point to them. To create a resource, you first have to create a resource handle and then tell it to create the resource. The following snippet uses resource handles to create a project, a folder, and a file.

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project  = root.getProject("MyProject");
IFolder folder = project.getFolder("Folder1");
IFile file = folder.getFile("hello.txt");
//at this point, no resources have been created
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!folder.exists()) 
    folder.create(IResource.NONE, true, null);
if (!file.exists()) {
    byte[] bytes = "File contents".getBytes();
    InputStream source = new ByteArrayInputStream(bytes);
    file.create(source, IResource.NONE, null);
}

This example defensively checks that the resource doesn’t already exist before trying to create it. This kind of defensive programming is a good idea because an exception is thrown if you try to create a resource that already exists. This way, the example can be run more than once in the same workspace without causing an error. The null parameters to the creation methods should be replaced by a progress monitor in a real application.


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.