-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamEx1.java
More file actions
59 lines (46 loc) · 1.25 KB
/
StreamEx1.java
File metadata and controls
59 lines (46 loc) · 1.25 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
package com.java8.streams;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
// program to show working of try with resources
public class StreamEx1 {
public static void main(String[] args) throws IOException {
// in java upto 7 , the files has to be explicitly closed. Failing which the lock for the file is taken
// data will not be persisted in the file
FileWriter fw = null ;
BufferedWriter bw = null ;
try{
fw = new FileWriter("sample.txt");
bw = new BufferedWriter(fw);
bw.write("Hello");
bw.newLine();
bw.write("how");
bw.newLine();
bw.write("are");
bw.newLine();
bw.write("you");
bw.newLine();
System.out.println("Data Written... ");
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try {
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// jdk 1.8
try(
FileWriter fw1 = new FileWriter("sample1.txt");
BufferedWriter bw1= new BufferedWriter(fw1);
){
bw1.write("This is in java 8 version ");
bw1.newLine();
bw1.write("Another line for test");
bw1.newLine();
}
}
}