Read File From Classpath in Java

Sheeraz Gul Apr 28, 2022
  1. Read File From Classpath in Java
  2. Use the getResourceAsStream() Method to Read Files From Classpath in Java
  3. Use the getResource() Method to Read Files From Classpath in Java
Read File From Classpath in Java

There are two ways to read files from classpath in Java. We can load the file present in the resource folder as inputstream or load it as URL format and then do whatever we want.

This tutorial demonstrates how to read a file from classpath in Java.

Read File From Classpath in Java

As mentioned above there are two methods, one is obj.getClass().getClassLoader().getResourceAsStream() and the other is obj.getClass().getClassLoader().getResource().

These methods can get the stream or URL or the corresponding file and then read the file. Before applying these operations, we must consider two points:

  1. We have to declare an object of the public class because the getClass() method is non-static, so we need an object to call it.
  2. The classpath is the same as where the example is running, so your file must be in the correct path.

Let’s start with the getResourceAsStream() method first.

Use the getResourceAsStream() Method to Read Files From Classpath in Java

package delftstack;

import java.io.*;

public class Read_Files {

    public static void main(String[] args) throws Exception {

		// creating object of the class for getClass method
        Read_Files Class_Object = new Read_Files();

        String Resource_File = "delftstack.txt";

        System.out.println("Reading the file " + Resource_File+ " from classpath");

        InputStream Input_Stream = Class_Object.getClass().getClassLoader().getResourceAsStream(Resource_File);
        InputStreamReader Input_Stream_Reader = new InputStreamReader(Input_Stream);
        BufferedReader reader = new BufferedReader(Input_Stream_Reader);

        String Content;

        while ((Content = reader.readLine()) != null) {
            System.out.println(Content);
        }
    }
}

The code above uses the getResourceAsStream method to get the inputstream and then read the file from the classpath with the help of BufferedReader. If the file is not in the classpath, it will return a null pointer exception. See output:

Reading the file delftstack.txt from classpath
Hello This is delftstack.com
The best online platform for learning different programming languages.

Use the getResource() Method to Read Files From Classpath in Java

package delftstack;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.nio.charset.StandardCharsets;

public class Read_Files {

    public static void main(String[] args) throws Exception {

        // creating object of the class for getClass method
        Read_Files Class_Object = new Read_Files();

        String Resource_File = "delftstack.txt";
        System.out.println("Reading the file " + Resource_File + " from classpath");

        URL Class_URL = Class_Object.getClass().getClassLoader().getResource(Resource_File);
        File Class_File = new File(Class_URL.toURI());
        List<String> Content;
        Content = Files.readAllLines(Class_File.toPath(), StandardCharsets.UTF_8);

        for(String Line: Content) {
            System.out.println(Line);
        }
    }
}

The code above uses the getResource() method to get the URL to the classpath and then read the file with the help of the URL. See output:

Reading the file delftstack.txt from classpath
Hello This is delftstack.com
The best online platform for learning different programming languages.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook