How to add Eclipse resource folder and files

Eclipse Resource Folder

 522 total views

In this blog, we will learn about resource folder in eclipse JAVA project and will see addition, deletion and access of these files and folders.

In the previous blog, I have explained How to pass arguments to JAVA’s main method in eclipse.

What is a resource?

A JAVA class works on many types of files to perform different tasks. For example Image, Text or Properties file.

A Java project treats the above files as a resource.

These files must be present in the classpath folder of the project.

Additionally, For their availability to class files the resource files must be packed into Java Archives(Jar).

What is the Resource Folder?

Java project creates dedicated folders for resource files and stores them in this folder.

You can always have more than one resource folder.

For example, A developer can create a dedicated folder for properties file and one for the images.

Method 1 | Adding the Eclipse Resource folder

Let’s see How can we add a resource folder in an eclipse project.

Right-Click on JAVA project, select Build path => Configure build path on the context menu.

Configure Build Path
Configure Build Path on Context Menu

Click on Add Folder… on the Java Build Path window

Add Folder
Add Folder

Click on Create New Folder… button on Source Folder Selection window

Create New Folder
Create a New Folder

Provide Folder Name in New Source Folder window and click Finish.

New Source Folder Name
Folder Name

Then Click OK and Apply and Close in subsequent windows.

The “resource” folder will be added to JAVA Project.

Eclipse resource folder added
resource folder in the project

Let’s add a file in the resource folder

Reading a file from resource folder

Below is the basic code to read a property file from the main method.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class PrintMessage {

	public static void main(String[] args) {
		InputStream inputStream = 
				PrintMessage.class.getResourceAsStream("/messages.properties");
		if (inputStream == null)
		{
			System.out.println("Input Stream is null");
			return;
		}
        String result = null;
		try {
			result = inputStreamToString(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
        System.out.println(result);
	}
	
    private static String inputStreamToString(InputStream aInInputStream)
    		throws IOException {

        ByteArrayOutputStream byteArrayOutputStream =
        		new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        while ((length = aInInputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, length);
        }

        return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());

    }
}

On running above code we will get below output

HelloMesssage= Hello, How are you?

Additionally, there is one more method to add a folder in the JAVA project.

Method 2 | Adding Folder Directly into the project

Right-Click on the project, Select New => Folder

Add  Eclipse resource Folder from New Menu

Let’s name this folder as properties and Click Finish.

Add Eclipse resource Folder directly
New Folder Window

Adding a new property file db.properties in it

DB property file

So, When I try to access this file using the same above code I get the below message.

Input Stream is null

The Main method is not able to read the new property file.

But Why?

Look closely at the icons of both the resource and properties folder. The icons on both the folder are different.

Actually the second folder is added into the project but it’s not on the ClassPath Therefore db.properties file is not accessible to the main.

So What can we do? Can we change a normal folder to a resource folder?

The Answer is Yes we can.

All you need to do is Right-Click on the properties folder then go to build path and Click Use as Source Folder.

Eclipse resource folder

Now Let’s Check the folder icons again

Converted Eclipse resource folder

Here both folders have similar icons.

Also, the above code will run successfully for db.properties file and it will print the file content.

threadcount= 1000

Conclusion

In Conclusion, I would say adding files to a resource folder and then reading its content is an easy task but it may trouble a newbie if he’s not very aware of the folder and Java Classpath concepts.

That is why I have tried to add the basic concept of the resource folder in this article.

Leave a Reply

Your email address will not be published. Required fields are marked *