1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* Read file using FileInputStream This example shows how to read a file using Java FileInputStream class. FileInputStream is used to read binary content of the file and return bytes of data. */ import java.io.*; public class ReadStringFromFile { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadString.txt"); int ch; StringBuffer strContent = new StringBuffer(""); FileInputStream fin = null; try { /* * Create new FileInputStream object. Constructor of FileInputStream throws * FileNotFoundException if the agrument File does not exist. */ fin = new FileInputStream(file); /* * To read bytes from stream use, * int read() method of FileInputStream class. * * This method reads a byte from stream. This method returns next byte of data * from file or -1 if the end of the file is reached. * * Read method throws IOException in case of any IO errors. */ while( (ch = fin.read()) != -1) strContent.append((char)ch); /* * To close the FileInputStream, use * void close() method of FileInputStream class. * * close method also throws IOException. */ fin.close(); } catch(FileNotFoundException e) { System.out.println("File " + file.getAbsolutePath() + " could not be found on filesystem"); } catch(IOException ioe) { System.out.println("Exception while reading the file" + ioe); } System.out.println("File contents :"); System.out.println(strContent); /* * Please note that, FileInputStream SHOULD NOT BE USED to read * character data file. * It is meant for reading binary data such as an image file. * * To read character data, FileReader should be used. */ } } /* Output would be File contents : This file is a demonstration of how to read a file using Java FileInputStream. */ |
i did not understand
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. “
worked wonders, thanks a lot
Cant we use readLine()
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
i found it so simple to learn difficult streams
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));
}
}
Thanks! I got mine working with the help of this example!
I want for loop logics and also mathematics calculations using java plz give me
gimmegimmegimeegimeegimme
Thanks This is an Awesome Tutorial 🙂
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”);
}
}
}
hey pls check your version,does it support scanner or not..
if it is Jdk5.0 means it wil work .. thats my ponit of view..
bt m nt sure frnd..
What kind of error are you getting. Add your error so we can take a closer look at the problem.