We are moved to new domain
Click -> www.ehowtonow.com
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

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: eHowToNow