Read file using FileInputStream

  1. /*
  2.   Read file using FileInputStream
  3.   This example shows how to read a file using Java FileInputStream class.
  4.   FileInputStream is used to read binary content of the file and
  5.   return bytes of data.
  6. */
  7. import java.io.*;
  8.  
  9. public class ReadStringFromFile {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. //create file object
  14. File file = new File("C://FileIO//ReadString.txt");
  15.  
  16. int ch;
  17. StringBuffer strContent = new StringBuffer("");
  18. FileInputStream fin = null;
  19.  
  20. try
  21. {
  22. /*
  23.   * Create new FileInputStream object. Constructor of FileInputStream throws
  24.   * FileNotFoundException if the agrument File does not exist.
  25.   */
  26.  
  27. fin = new FileInputStream(file);
  28.  
  29. /*
  30.   * To read bytes from stream use,
  31.   * int read() method of FileInputStream class.
  32.   *
  33.   * This method reads a byte from stream. This method returns next byte of data
  34.   * from file or -1 if the end of the file is reached.
  35.   *
  36.   * Read method throws IOException in case of any IO errors.
  37.   */
  38.  
  39. while( (ch = fin.read()) != -1)
  40. strContent.append((char)ch);
  41.  
  42. /*
  43.   * To close the FileInputStream, use
  44.   * void close() method of FileInputStream class.
  45.   *
  46.   * close method also throws IOException.
  47.   */
  48. fin.close();
  49.  
  50. }
  51. catch(FileNotFoundException e)
  52. {
  53. System.out.println("File " + file.getAbsolutePath() +
  54. " could not be found on filesystem");
  55. }
  56. catch(IOException ioe)
  57. {
  58. System.out.println("Exception while reading the file" + ioe);
  59. }
  60.  
  61. System.out.println("File contents :");
  62. System.out.println(strContent);
  63.  
  64.  
  65. /*
  66.   * Please note that, FileInputStream SHOULD NOT BE USED to read
  67.   * character data file.
  68.   * It is meant for reading binary data such as an image file.
  69.   *
  70.   * To read character data, FileReader should be used.
  71.   */
  72. }
  73. }
  74.  
  75. /*
  76. Output would be
  77. File contents :
  78. This file is a demonstration of how to read a file using Java FileInputStream.
  79. */

do while

i did not understand

what's the purpose of FileInputStream

I can't figure out why not just use FileReader. Using FileInputStream makes no sense. It saves a file with asci/unicode numbers in it, instead of actual characters. that is the only difference, so then FileReader would always be better than FileInputStream.

thanks

worked wonders, thanks a lot

read

Cant we use readLine()

simple programe

i found it so simple to learn difficult streams

text fields in student grade

hello can you help me with my project here... i didnt know why it cant compute for the average grade of the student...please do help me!

the codes are as follows!!!
help me pls i am begging!!!

import java.util.Scanner;
import java.io.*;

public class output{
public static void main (String[] args)throws FileNotFoundException {

PrintWriter out = null;
Scanner reader=null;

String Fname,Lname;
int IT6,IT7,IT8,IT9,IT10;
int average;

try{
out = new PrintWriter(new FileOutputStream("d:\\outputofDATA.txt",true));
reader=new Scanner(new FileInputStream("d:\\input.txt"));

while(reader.hasNext()){

Fname=reader.next();
Lname=reader.next();

IT6=reader.nextInt();
IT7=reader.nextInt();
IT9=reader.nextInt();
IT8=reader.nextInt();
IT10=reader.nextInt();

average=reader.nextInt();
average=((IT6+IT7+IT8+IT9+IT10)/5);

out.println("Name :"+Fname);
out.println("Last name : "+Lname);
out.println("Grade in IT 6: "+IT6);
out.println("Grade in IT 7: "+IT7);
out.println("Grade in IT 8: "+IT8);
out.println("Grade in IT 9: "+IT9);
out.println("Grade in IT 10: "+IT10);

out.printf("The average grade is: "+average);

}
System.out.println ("Done");

out.close();
reader.close();
}catch(FileNotFoundException e){
System.out.println ("error");
}
}
}

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!