Skip to content

Commit f3b5187

Browse files
committed
File Hanlding | Character Stream Code
1 parent 958f76f commit f3b5187

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

FileHandling/ByteStream.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
3+
public class ByteStream
4+
{
5+
public static void main(String[] args) throws IOException{
6+
FileInputStream in = null;
7+
FileOutputStream out = null;
8+
9+
try{
10+
in = new FileInputStream("input.txt");
11+
out = new FileOutputStream("output.txt");
12+
int c;
13+
/**the byte is returned as an int in the range of 0 to 255.
14+
* If no value is available and end of the stream has been reached
15+
* then it returns -1.
16+
*/
17+
while ((c = in.read()) != -1)
18+
{
19+
out.write(c);
20+
}
21+
}
22+
finally{
23+
if(in != null){
24+
in.close();
25+
}
26+
if(out != null){
27+
out.close();
28+
}
29+
}
30+
}
31+
}

FileHandling/CharacterStream.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
3+
public class CharacterStream{
4+
public static void main(String[] args) throws IOException{
5+
FileWriter writer = new FileWriter("character_stream.txt");
6+
writer.write("Written from File Writer");
7+
writer.close();
8+
9+
FileReader fr = new FileReader("character_stream.txt");
10+
11+
/**Create a new array 'a' with dataType char and space 50 */
12+
char[] a = new char[50];
13+
fr.read(a);
14+
15+
/**Every element of the array a is represented by the variable 'c' */
16+
for(char c : a)
17+
System.out.println(c);
18+
fr.close();
19+
}
20+
}

FileHandling/character_stream.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Written from File Writer

FileHandling/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello this is Input. See you in the Output file.

FileHandling/output.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello this is Input. See you in the Output file.

0 commit comments

Comments
 (0)