/*
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.
*/
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");
}
}
}