-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReadTest.java
More file actions
107 lines (83 loc) · 1.85 KB
/
Copy pathFileReadTest.java
File metadata and controls
107 lines (83 loc) · 1.85 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class FileReadTest {
public static void main(String[] args) {
String s = "C:\\Users\\VBR12\\Desktop\\batch.txt";
MyFileReader mfr = new MyFileReader();
mfr.readFile(s);
s= "C:\\Users\\VBR12\\Desktop\\batch2.txt";
mfr.readFile(s);
mfr.writeFile();
s="C:\\Users\\VBR12\\Desktop\\writeFile.txt";
mfr.readFile(s);
try
{
FileReader fr = new FileReader("C:\\Users\\VBR12\\Desktop\\writeFile.txt");
System.out.println("**********In file reader////");
int i =fr.read();
while(i!=-1)
{
System.out.print((char)i);
i = fr.read();
}
fr.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class MyFileReader
{
FileInputStream fin;
void readFile(String filename)
{
try
{
fin = new FileInputStream(filename);
byte b = (byte)fin.read();
System.out.print((char)b);
while(b != -1)
{
b = (byte)fin.read();
System.out.print((char)b);
}
fin.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("File closed");
}
void writeFile()
{
try
{
FileOutputStream fos = new FileOutputStream("C:\\Users\\VBR12\\Desktop\\writeFile.txt",true);
System.out.println("Enter the text: ");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
byte bytes[] = s.getBytes();
fos.write(bytes);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}