-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInputStream.java
More file actions
40 lines (35 loc) · 1.17 KB
/
FileInputStream.java
File metadata and controls
40 lines (35 loc) · 1.17 KB
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
package stream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class InputStream {
public static void main(String[] args) {
try{
// Using file FileInputStream Class
FileInputStream file = new FileInputStream("C:\\MyJava\\Data.txt");
// By reading a character at a time
int x = 0;
while((x=file.read())!=-1){
System.out.print((char)x);
}
System.out.println();
file.close();
FileInputStream fileOpen = new FileInputStream("C:\\MyJava\\Data.txt");
//By converting bytes into String
byte b[] = new byte[fileOpen.available()];
fileOpen.read(b);
String s = new String(b);
System.out.println(s);
// Using file FileReader Class
FileReader fileRead = new FileReader("C:\\MyJava\\Data.txt");
int y = 0;
while((y= fileRead.read())!=-1){
System.out.print((char)y);
}
}
catch(IOException exception)
{
System.out.println(exception);
}
}
}