java.lang.Class.getResourceAsStream()


java.lang.Class.getResourceAsStream()

package com.codingdict;



import java.lang.*;

import java.io.*;



public class ClassDemo {



   static String getResource(String rsc) {

      String val = "";



      try {

         // input stream

         InputStream i = ClassDemo.class.getResourceAsStream(rsc);

         BufferedReader r = new BufferedReader(new InputStreamReader(i));



         // reads each line

         String l;

         while((l = r.readLine()) != null) {

            val = val + l;

         } 

         i.close();

      } catch(Exception e) {

         System.out.println(e);

      }

      return val;

   }



   public static void main(String[] args) {



      System.out.println("File1: " + getResource("file.txt"));

      System.out.println("File2: " + getResource("test.txt"));                

   }

}