How to read a file from classpath in java?

Use the following code to read a file from classpath in Java.

InputStreamReader fl = new InputStreamReader( M2MSqlScriptGenerator.class.getClassLoader()
    .getResourceAsStream("techdive.txt"));
               
BufferedReader bufReader = new BufferedReader(fl);

String line = null;
while((line = bufReader.readLine())!=null)
{
        System.out.println(line);
}

The file techdive.txt should be present in the classpath.

Search