Skip to content

Commit b8c8ef4

Browse files
authoredNov 9, 2021
add ObjectStream and RandomAccess (#203)
* add ObjectStream and RandomAccess * fixed gradle building
1 parent 56913b9 commit b8c8ef4

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class ObjectOutputInputStreamExample {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.examplehub.basics.io;
2+
3+
public class RandomAccessFileExample {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 User implements Serializable{
12+
//@Serial TODO
13+
private static final long serialVersionUID = 100L;
14+
private final String username;
15+
private final String password;
16+
private final int age;
17+
18+
public User(String username, String password, int age) {
19+
this.username = username;
20+
this.password = password;
21+
this.age = age;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "User{" +
27+
"username='" + username + '\'' +
28+
", password='" + password + '\'' +
29+
", age=" + age +
30+
'}';
31+
}
32+
}
33+
class ObjectOutputInputStreamExampleTest {
34+
@Test
35+
void testWriteRead() throws IOException, ClassNotFoundException {
36+
37+
User user = new User("root", "112233", 25);
38+
String filename = "user.db";
39+
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
40+
oos.writeObject(user);
41+
}
42+
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
43+
User readUser = (User) ois.readObject();
44+
assertEquals(user.toString(), readUser.toString());
45+
}
46+
assertTrue(Files.deleteIfExists(Paths.get(filename)));
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.examplehub.basics.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.PrintStream;
8+
import java.io.RandomAccessFile;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
class RandomAccessFileExampleTest {
16+
@Test
17+
void testSeek() throws IOException {
18+
String filename = "example.txt";
19+
try(PrintStream printStream = new PrintStream(new FileOutputStream(filename));
20+
RandomAccessFile randomAccessFile = new RandomAccessFile(filename, "rw")) {
21+
printStream.write("Java".getBytes(StandardCharsets.UTF_8));
22+
printStream.flush();
23+
24+
assertEquals('J', randomAccessFile.read());
25+
randomAccessFile.seek(2);
26+
assertEquals('v', randomAccessFile.read());
27+
randomAccessFile.write("123".getBytes(StandardCharsets.UTF_8));
28+
randomAccessFile.seek(0);
29+
assertEquals("Jav123", randomAccessFile.readLine());
30+
}
31+
assertTrue(Files.deleteIfExists(Paths.get(filename)));
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.