Skip to content

Commit d80c742

Browse files
io operation (#202)
* refactor directory * list files recursion * io operation
1 parent 06e84d8 commit d80c742

16 files changed

+255
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class BufferInputStreamExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class BufferReaderExample {
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.examplehub.basics.io;
2+
3+
public class DataInputStreamExample {
4+
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class DataOutputStreamExample {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class PrintStreamExample {
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.examplehub.basics.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class BufferInputStreamExampleTest {
12+
@Test
13+
void testCopyFile() throws IOException {
14+
String filename = "pom.xml";
15+
String newFileName = "pom_bk.xml";
16+
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
17+
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFileName))) {
18+
byte[] buffer = new byte[1024];
19+
int readBytes;
20+
while ((readBytes = bis.read(buffer)) != -1) {
21+
bos.write(buffer, 0, readBytes);
22+
bos.flush();
23+
}
24+
assertEquals(new File(filename).length(), new File(newFileName).length());
25+
}
26+
assertTrue(Files.deleteIfExists(Paths.get(newFileName)));
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.examplehub.basics.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class BufferReaderExampleTest {
12+
@Test
13+
void testCopyFile() throws IOException {
14+
String filename = "pom.xml";
15+
String newFileName = "pom_bk.xml";
16+
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));
17+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newFileName))) {
18+
String line = bufferedReader.readLine();
19+
while (line != null) {
20+
String nextLine = bufferedReader.readLine();
21+
bufferedWriter.write(line);
22+
if (nextLine != null) {
23+
bufferedWriter.write(System.lineSeparator());
24+
}
25+
bufferedWriter.flush();
26+
line = nextLine;
27+
}
28+
assertEquals(new File(filename).length(), new File(newFileName).length());
29+
}
30+
assertTrue(Files.deleteIfExists(Paths.get(newFileName)));
31+
}
32+
}

src/test/java/com/examplehub/basics/io/CreateFolderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
6+
import com.examplehub.basics.io.CreateFolder;
7+
58
import org.junit.jupiter.api.Disabled;
69
import org.junit.jupiter.api.Test;
710

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.examplehub.basics.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class DataInputStreamExampleTest {
12+
@Test
13+
void testRead() throws IOException {
14+
String filename = "example.txt";
15+
try (DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(filename));
16+
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(filename))) {
17+
dataOutputStream.writeInt(97);
18+
dataOutputStream.flush();
19+
20+
assertEquals(97, dataInputStream.readInt());
21+
}
22+
assertTrue(Files.deleteIfExists(Paths.get(filename)));
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.basics.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.DataOutputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class DataOutputStreamExampleTest {
14+
@Test
15+
void testWriteInt() throws IOException {
16+
String filename = "example.txt";
17+
try(DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(filename))) {
18+
dataOutputStream.writeInt(97);
19+
}
20+
assertTrue(Files.deleteIfExists(Paths.get(filename)));
21+
}
22+
}

0 commit comments

Comments
 (0)