728x90 AdSpace

Tuesday, 1 October 2013

Reading the file using FileInputStream


In this tutorials we are going to see how to read file using FileInputStream.
  1. Create project called JavaMisc.
  2. Create package called com.javatutorialscorner.io
  3. Create java class called FileReader under com.javatutorialscorner.io package.
FileReader.java
package com.javatutorialscorner.io;

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

public class FileReader {

public static void main(String[] args) {
File file = new File("C:\\jtc\\javatutorialscorner.txt");
FileInputStream fin = null;
int templine;
try {
fin = new FileInputStream(file);

while ((templine = fin.read()) != -1) {
System.out.print((char) templine);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

Now run the program see the following output in console.

this is test file created by java tutorials corner

This content read from input file
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Reading the file using FileInputStream Rating: 5 Reviewed By: Annamalai Thangaraj