Adding a File

When you add a file to your project, it is included in the project's build process. Adding a file to a Java project also adds the file to the project directory structure on the hard disk:

  • Creating a new file in the project creates a new file on the disk.

  • Adding a file that already exists on the disk copies the file to the appropriate location in the project directory structure.

Note   Some files may reside in the project directory structure without belonging to the project. For example, removing a file from the project still leaves the file in the project directory structure. This file can be added back to the project in its same location on the disk.

You can add a file to the top-level project or to a specific folder. For information about adding folders to the project, see Adding a Folder.

To add a file to the project

  1. In Project Explorer, select the project or folder node that will contain the file.

  2. On the Project menu, click Add Item.

  3. To create a new file, click the New tab in the Add Item dialog box.

    • In the left pane of the dialog box, select a file category. In the right pane, select the type of file you want to add, based on the selection in the left pane.

    • In the Name box, type a name for the file. For information about valid Java file names, see in the Java Language Specification.

    • Click Open. Note that when you add a new .java file, Visual J++ automatically inserts the proper package statement into the file. (If you add a .java file to the project node itself, no package statement is inserted, since the project node is considered the default package.)

  4. To add an existing file, click the Existing tab in the Add Item dialog box.

    • Find and select the file you want to add. You can select multiple files from the same folder by using the SHIFT or CTRL keys.

    • Click Open. Note that when you add an existing .java file, you must manually change its package statement to reference the new package. (Note that the project node itself is considered the default package.) If a file's package statement does not map to the folder it resides in, the output directory structure will not match the source directory structure when you build the project.

When a single file is added to the project through Project Explorer, it is automatically opened in its default editor. When multiple files are added at the same time, no file is opened. For information about opening files, see Opening a File.

Note   If a file is added to the project directory structure through the file system, it is automatically added to the project if its file type is specified in the project filter. The file is then displayed in Project Explorer, but not opened. For more information about the project filter, see Displaying All Files in the Project Directory Structure.

Adding a File that Resides in the Project Directory Structure

If a file does not belong to the project, but it already exists in the project directory structure, you can simply add it to the project in that same location.

To add a file that already resides in the project directory structure

  1. Set the project view to directory view by clicking the Directory View button in Project Explorer. Only directory view allows you to see the nonproject files that reside in the project directory structure. For more information about views, see Choosing the Project View.

  2. If the nonproject files are not currently displayed, click the Show All Files button in Project Explorer to show all files in the project directory structure.

  3. Right-click the name of the file that you want to add to the project. Note that you cannot add a file if its folder is not in the project. For information about adding the folder, see Adding a Folder that Resides in the Project Directory Structure.

  4. On the shortcut menu, click Add To Project. The file is added to the project, but not opened. For information about opening files, see Opening a File.

  5. To hide any remaining nonproject files, click the Show All Files button again.

For more information about nonproject files that reside in the project directory structure, see Displaying All Files in the Project Directory Structure.