In this tutorials we are going to see how to read file using FileInputStream.
- Create project called JavaMisc.
- Create package called com.javatutorialscorner.io
- Create java class called FileReader under com.javatutorialscorner.io package.
package com.javatutorialscorner.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) {
File file = new File("C:\\jtc\\javatutorialscorner.txt");
FileInputStream fin = null;
int templine;
try {
fin = new FileInputStream(file);
while ((templine = fin.read()) != -1) {
System.out.print((char) templine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.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
0 comments:
Post a Comment