Home » Java programming language

Java Class class getResourceAsStream() method with example

Class class getResourceAsStream() method: Here, we are going to learn about the getResourceAsStream() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 14, 2019

Class class getResourceAsStream() method

  • getResourceAsStream() method is available in java.lang package.
  • getResourceAsStream() method is used to get the resource as a parameter and convert the resource into InputStream.
  • getResourceAsStream() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getResourceAsStream() method may throw an exception at the time of returning InputStream object.
    NullPointerException: This exception may raise when the given resource_name is null.

Syntax:

    public InputStream getResourceAsStream(String resource_name);

Parameter(s):

  • String resource_name – represents the name of the resource.

Return value:

The return type of this method is InputStream, it returns the following values based on the following cases,

  • It returns the InputStream, when any resource associated with the given name exists.
  • It returns null, when no resource associated with the given name exists.

Example:

// Java program to demonstrate the example 
// of InputStream getResourceAsStream(String resource_name) 
// method of  Class 

import java.io.*;
import java.util.*;

public class GetResourceAsStreamOfClass {
    public static String resource(String res) {
        String count = "";
        try {

            Class cl = Class.forName("GetResourceAsStreamOfClass");
            ClassLoader loader = cl.getClassLoader();

            // By using getResourceAsStream() method is to take the resource
            // and convert it into InputStream
            InputStream is = loader.getResourceAsStream(res);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) != null) {
                count = count + 1;
            }
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return count;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(resource("Includehelp.doc"));
    }
}

Output

Includehelp is a technical site






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.