forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWrite.java
More file actions
58 lines (48 loc) · 1.5 KB
/
FileWrite.java
File metadata and controls
58 lines (48 loc) · 1.5 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
package coding;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.testng.annotations.Test;
public class FileWrite extends BaseTestNg{
String fileName ="./NewFile.txt";
@Test(priority=1)
public void usingOutputStream() throws FileNotFoundException {
try {
FileOutputStream writer=new FileOutputStream(fileName);
writer.write("TestLeaf Software".getBytes()); // You have to convert to bytes and write them
writer.write("\r\n".getBytes()); // write new line
writer.write("Welcome to the learning.".getBytes());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(priority=2)
public void usingBufferWriter() {
FileWriter fw;
try {
fw = new FileWriter(fileName);
BufferedWriter writer = new BufferedWriter(fw);
writer.write("TestLeaf Software");
writer.write("\r\n"); // write new line
writer.write("Welcome to the learning.");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test(priority=3)
public void usingFileWriter() throws FileNotFoundException {
try {
FileWriter writer = new FileWriter(fileName, true);
writer.write("TestLeaf Software");
writer.write("\r\n"); // write new line
writer.write("Welcome to the learning.");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}