FileInputStream

Read file using FileInputStream

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

19 Comments

  • 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.

    • FileReader is not always better than a FileInputStream.
      FileReader is used for reading in characters while
      FileInputStream is used for reading raw data such as image files, video files etc.

      Read the Java API….
      FileReader: “Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream. “

      FileInputStream: “A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. “

    • FileReader is not always better than a FileInputStream.
      FileReader is used for reading in characters while
      FileInputStream is used for reading raw data such as image files, video files etc.

      Read the Java API….
      FileReader: “Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream. “

      FileInputStream: “A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. “

    • When we are dealing with “BufferedReader”, its opt to use readLine(). Analyze the following small piece of code for your understanding.

      BufferedReader bufferreader = new BufferedReader(new FileReader(args[i]));
      while ((thisLine = bufferreader.readLine()) != null)

      Hope I helped you 🙂

      regards
      Rajesh Muralidharen

  • Hi

    If I’m accessing a file on a network location using FileInputStream, how can I use my username/password with FileInputStream so I can get access to the file.

    Please help.
    Thanks

    • package sor;
      import java.io.*;
      import java.math.BigInteger;

      public class Long {

      public static void main(String[] args)throws IOException{

      System.out.println(Math.pow(2,2));
      System.out.println(Math.pow(2,3));
      }

      }

    • package sor;
      import java.io.*;
      import java.math.BigInteger;

      public class Long {

      public static void main(String[] args)throws IOException{

      System.out.println(Math.pow(2,2));
      System.out.println(Math.pow(2,3));
      }

      }

  • Pointless. Why post an example that shows how it is NOT done? Either have it use StringBuffer and FileReader OR have it use e.g. an int array and FileInputStream.

    Bad example is bad.

  • 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”);
    }
    }
    }

Sponsors

Facebook Fans