How to read file in Java – FileInputStream

In Java, FileInputStream is a bytes stream class that’s used to read bytes from file. The following example will use FileInputStream to read a file named “c:/robots.txt” and display its content to console.


//This is content of file : c:/robots.txt
User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-includes-test/

See below full example.


package com.mkyong.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileExample {

	public static void main(String[] args) {

		File file = new File("C:/robots.txt");
		FileInputStream fis = null;

		try {
			fis = new FileInputStream(file);

			System.out.println("Total file size to read (in bytes) : "
					+ fis.available());

			int content;
			while ((content = fis.read()) != -1) {
				// convert to char and display it
				System.out.print((char) content);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}

Following result will be displayed on console.


Total file size to read (in bytes) : 90
User-agent: *
Disallow: /wp-admin/
Disallow: /wp-includes/
Disallow: /wp-includes-test/

JDK 7 example

An updated JDK7 example, using new “try resource close” method to handle file easily.


package com.mkyong.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileExample {

	public static void main(String[] args) {

		File file = new File("C:/robots.txt");

		try (FileInputStream fis = new FileInputStream(file)) {

			System.out.println("Total file size to read (in bytes) : "+ fis.available());

			int content;
			while ((content = fis.read()) != -1) {
				// convert to char and display it
				System.out.print((char) content);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

References

  1. http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileInputStream.html
  2. http://www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Jenny
Guest
Jenny

Nice post but can I have some simple examples?

whitedude22
Guest
whitedude22

thanks mkyong!

Samundi
Guest
Samundi

Nice

owais
Guest
owais

@mkyong If i want to pick a specific word or String from a file ,So then what can I do?

Florin Marcus
Guest
Florin Marcus

If you are posting java solution, please try to implement the basics of exception handling, people will copy this code into their sources.

Thomas androws
Guest
Thomas androws

Nice post but can I have some simple examples?

Robert Carter
Guest
Robert Carter
Hi, Available does not return the file size, it returns the size of the internal buffer allowed before the process must be locked, try a large (>2gb file) and you’ll see what I am talking about. Here is a method to get the file size: import java.io.File; public long getFileSize() { File file = new File(_filePath); if (!file.exists() || !file.isFile()) { System.out.println(“File doesn\’t exist”); return -1; } //Here we get the actual size return file.length(); } Hope that helps!
Leyland Cypress
Guest
Leyland Cypress

It does help. Thanks. You know what else though? There ought to be some discussion here of how to get the file path when working in a web environment.

Justin James P
Guest
Justin James P

Nenga oru Village vignani Bass…

justin
Guest
justin

en mugathu munnadi puzhalrathu enakku pidikathu..

dra
Guest
dra

neega sonna sari dhan boss

trackback
Rounding off Decimal Numbers to the Nearest Hundredth

[…] […]