Reading the Resource or Property files in Java as a file stream and outputting the content to console

A good programming practice is to avoid hardcoding values in your Java File. These values should be read from some resource files or proerty files. The major benefit of doing that is that in case you decide to alter those values, there is no need to recompile your classes. Another benefit is the isolation of the resources from the logic.

Is it difficult to read the resource from a file on to the Java class? No, it’s pretty simple. The following sample code that I wrote shows how easily you can read the content of your file as a stream. How you use the value thus read in your Java file is up to you. I have only demonstrated how the resource is actually read.

This program does the following:

  • Read resource file as a stream from Java class
  • Print the content of the resource file onto the Java console.

 * @author Kushal Paudyal
 * Created on 2012-07-03
 * Last Modified on 2012-07-03
 * This class demonstrate how we can read the resource files as file input stream.
 * - Reads a resource file
 * - Prints file contests on to the console.


public class ReadResourceFile {

	public static void main(String args []) throws Exception {

	private static void readResourceFileAndPrintContents() throws Exception {
		/** Get the input stream for the resource file**/
		InputStream stream = loadResourceAsStream ("C:/temp/");

		/**Create a BufferedReader object for the input stream**/
		BufferedReader in = new BufferedReader(new InputStreamReader(stream));

		/**Read the lines and print them onto the console**/
		String line;
	    while ((line = in.readLine()) != null) {

	 * This method
	 * - Takes a resource file name as parameter
	 * - Returns the InputStream object of FileInputStream type
	public static InputStream loadResourceAsStream(final String resourceName) {
		InputStream input = null;
		try {
			input = new FileInputStream(resourceName);
		} catch (FileNotFoundException e) {
			System.out.println("Resource File Not Found");
		return input;


Content of the file c:/temp/

Here is the output of this program, which is same as the content of the file. The java class simply read the resource file as stream and spit out the content onto the console.

[My Video Promotion]
  1. How to read / write UTF8 and Non-UTF8 files in Java
  2. How To Print A Text File In Java
  3. How to read a tab separated or tab delimited file in Java program and print the content to console
  4. Computing the total, free and usable disk space easily using JDK 1.6
  5. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  6. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  7. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  8. Reading / Writing File in Java and String Manipulation
  9. How To Read DOC file Using Java and Apache POI
  10. Writing To Excel File Using Apache POI
  11. Reading Excel File Using Java And Apache POI
  12. Calculating Folder Size In Java
  13. How To Rename A File In Java
  14. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

6 Responses to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

  2. d says:

    would that help you tocreate the file from ubunt server

  3. komal gaikwad says:

    in this code i got problem of array out of bound plz tell me solution.

  4. komal gaikwad says:

    In this code i got error
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
    plz send me proper code….

  5. Neethu says:

    Thank you very much for the solution. This is the one which i was searching.

  6. Manikant says:

    I need to copy to the shared folder which is shared and can be authenticate by using credentials.

    but here the code is giving to write .

    I need copy my folder from my local to shared location

    Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.