Java Class .getResourceAsStream (String name)

Syntax

Class.getResourceAsStream(String name) has the following syntax.

public InputStream getResourceAsStream(String name)

Example

In the following code shows how to use Class.getResourceAsStream(String name) method.


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/*ww w . j a  va  2 s  .  c om*/
public class Main {
  public static void main(String[] args) throws Exception {
    String val = "";
    InputStream i = Main.class.getResourceAsStream("text.txt");
    BufferedReader r = new BufferedReader(new InputStreamReader(i));

    // reads each line
    String l;
    while ((l = r.readLine()) != null) {
      val = val + l;
    }
    i.close();
  }
}




















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable