forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriterExampleTest.java
More file actions
55 lines (49 loc) · 1.54 KB
/
FileWriterExampleTest.java
File metadata and controls
55 lines (49 loc) · 1.54 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
package com.examplehub.basics.io;
import static org.junit.jupiter.api.Assertions.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
class FileWriterExampleTest {
@Test
void testWrite() throws IOException {
String fileName = "example.txt";
try (Writer writer = new FileWriter(fileName)) {
writer.write("hello");
}
assertEquals("hello", FileReaderExample.read(fileName));
Files.deleteIfExists(Paths.get(fileName));
}
@Test
void testWriteSingleChar() throws IOException {
String fileName = "example.txt";
try (Writer writer = new FileWriter(fileName)) {
writer.write('A');
writer.write(66); // write B
}
assertEquals("AB", FileReaderExample.read(fileName));
Files.deleteIfExists(Paths.get(fileName));
}
@Test
void testWriteCharArray() throws IOException {
char[] letters = {'J', 'a', 'v', 'a'};
String fileName = "example.txt";
try (Writer writer = new FileWriter(fileName)) {
writer.write(letters);
}
assertEquals("Java", FileReaderExample.read(fileName));
Files.deleteIfExists(Paths.get(fileName));
}
@Test
void testWriteOnesOfCharArray() throws IOException {
char[] letters = {'J', 'a', 'v', 'a'};
String fileName = "example.txt";
try (Writer writer = new FileWriter(fileName)) {
writer.write(letters, 1, 2);
}
assertEquals("av", FileReaderExample.read(fileName));
Files.deleteIfExists(Paths.get(fileName));
}
}