Friday, February 25, 2011

Load file in classpath in JAVA

package com.java.discussions101;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Main {


public static void main(String[] args) throws FileNotFoundException, IOException {


Properties prop = new Properties();


//prop.load(ClassLoader.getSystemResourceAsStream("property/file.txt"));

prop.load(Main.class.getResourceAsStream("/property/file.txt"));


String str=prop.getProperty("");

System.out.println(str);
}
}


You can choose 2 ways to load files and read it in your code. You can choose either the ClassLoader or the Class.clazz to load file. In my case I have a director in my root named "property" and inside it a file named "file.txt" is there.

Cheers....

No comments:

Post a Comment