Reading a File from same package in a Java Class

Sometimes we may need to read data from a file in a Java class and this file lies in the same package as that of the class. Different Java classes may need to read data from a file lying in their own package. If all the files in different packages have the same name e.g. myprops.properties, then how to make sure that the Java classes read the file lying in their own package?

First we need to get the Class object from the given class and then we can use the getResourceAsStream(resource name) method of the Class object to get the resource from the same package as that of the class.

An absolute resource name is constructed from the given resource name as follows:-
1. If the resource name begins with a ‘/’, then the absolute name of the resource is the name following the ‘/’.
2. Otherwise, the absolute resource name is created as – modified_package_name/resource_name
Where the modified_package_name is the package name of this class with ‘.’ substituted by ‘/’.

Suppose we have 2 packages com.test1 and com.test2.

com.test1 package contains the following – FileReader1.java class and myprops.properties.

FileReader1.java :-

package com.test1;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileReader1 {

    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        InputStream is = FileReader1.class.getResourceAsStream("myprops.properties");
        
        props.load(is);
        
        System.out.println(props.getProperty("url"));
        System.out.println(props.getProperty("name"));
        System.out.println(props.getProperty("password"));
        
    }
}

myprops.properties :-

url=http://www.google.com
name=rajesh
password=zxsdsaasdddesddda23sxsz

If we run the Class FileReader1, we get the following output:-

http://www.google.com
rajesh
zxsdsaasdddesddda23sxsz

com.test2 package contains the following – FileReader2.java class and myprops.properties.

FileReader2.java :-

package com.test2;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FileReader2 {

    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        InputStream is = FileReader2.class.getResourceAsStream("myprops.properties");
        
        props.load(is);
        
        System.out.println(props.getProperty("url"));
        System.out.println(props.getProperty("name"));
        System.out.println(props.getProperty("password"));
        
    }
}

myprops.properties :-

url=http://www.facebook.com
name=shrikant
password=ksdfksjfkj23k43j2jsckdfjsk324

If we run the Class FileReader2, we get the following output:-

http://www.facebook.com
shrikant
ksdfksjfkj23k43j2jsckdfjsk324

We can see that each of the Java classes are reading data from the files lying in their respective packages.

Leave a Reply

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