Sunday 13, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 2 October 2013

Read File using BufferedInputStream


In this tutorials we are going to see how to read file using BufferedInputStream.

  1. Create project called JavaMisc.
  2. Create package called com.javatutorialscorner.io 
  3. Create java class called FileReaderExample under com.javatutorialscorner.io package.

FileReaderExample.java

package com.javatutorialscorner.io;

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

public class FileReaderExample {

public static void main(String[] args) {

File file = null;
FileInputStream fins = null;
BufferedInputStream bins = null;
DataInputStream din = null;

file = new File("C:\\jtc\\javatutorialscorner.txt");
try {
fins = new FileInputStream(file);
bins = new BufferedInputStream(fins);
din = new DataInputStream(bins);
while (din.available() != 0) {
System.out.println(din.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
fins.close();
bins.close();
din.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

Related Posts:

  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to get File Last modified Date in Java Following example shows how to get file last modified date using file.lastModified() method in Java. lastModified public long lastModified()  Returns the time that the file denoted by this abstract pathname was last mo… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to create temp file using Java Following example shows how to create temp file using file.createTempFile() method in Java. createTempFile public static File createTempFile(String prefix,String suffix) throws IOException Creates an empty file in the defaul… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Read File using BufferedInputStream Rating: 5 Reviewed By: eHowToNow