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.
01.
package
com.javatutorialscorner.io;
02.
03.
import
java.io.File;
04.
import
java.io.FileInputStream;
05.
import
java.io.IOException;
06.
07.
public
class
FileReader {
08.
09.
public
static
void
main(String[] args) {
10.
File file =
new
File(
"C:\\jtc\\javatutorialscorner.txt"
);
11.
FileInputStream fin =
null
;
12.
int
templine;
13.
try
{
14.
fin =
new
FileInputStream(file);
15.
16.
while
((templine = fin.read()) != -
1
) {
17.
System.out.print((
char
) templine);
18.
}
19.
20.
}
catch
(IOException e) {
21.
e.printStackTrace();
22.
}
finally
{
23.
try
{
24.
if
(fin !=
null
) {
25.
fin.close();
26.
}
27.
}
catch
(IOException e) {
28.
// TODO Auto-generated catch block
29.
e.printStackTrace();
30.
}
31.
}
32.
33.
}
34.
35.
}
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