Sunday 13, Apr 2025
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
01.package com.javatutorialscorner.io;
02. 
03.import java.io.File;
04.import java.io.FileInputStream;
05.import java.io.IOException;
06. 
07.public class FileReader {
08. 
09. public static void main(String[] args) {
10.  File file = new File("C:\\jtc\\javatutorialscorner.txt");
11.  FileInputStream fin = null;
12.  int templine;
13.  try {
14.   fin = new FileInputStream(file);
15. 
16.   while ((templine = fin.read()) != -1) {
17.    System.out.print((char) templine);
18.   }
19. 
20.  } catch (IOException e) {
21.   e.printStackTrace();
22.  } finally {
23.   try {
24.    if (fin != null) {
25.     fin.close();
26.    }
27.   } catch (IOException e) {
28.    // TODO Auto-generated catch block
29.    e.printStackTrace();
30.   }
31.  }
32. 
33. }
34. 
35.}

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

Related Posts:

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Reading the file using FileInputStream Rating: 5 Reviewed By: eHowToNow