Skip to content

Commit 9cc637a

Browse files
realDuYuanChaogithub-actions
andauthored
FileInputStream operation (#174)
* file operations * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 819ce32 commit 9cc637a

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.examplehub.basics.io;
2+
3+
import java.io.*;
4+
5+
public class FileInputStreamExample {
6+
public static void readFile(String filename) throws IOException {
7+
InputStream inputStream = null;
8+
try {
9+
inputStream = new FileInputStream(filename);
10+
int read;
11+
while ((read = inputStream.read()) != -1) {
12+
System.out.print((char) read);
13+
}
14+
} catch (IOException e) {
15+
e.printStackTrace();
16+
} finally {
17+
assert inputStream != null;
18+
inputStream.close();
19+
}
20+
}
21+
22+
public static void readFileWithTryRecourse(String filename) throws FileNotFoundException {
23+
try (InputStream inputStream = new FileInputStream(filename)) {
24+
int read;
25+
while ((read = inputStream.read()) != -1) {
26+
System.out.print((char) read);
27+
}
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
33+
public static void readFileWithBuffer(String filename) throws IOException {
34+
try (InputStream inputStream = new FileInputStream(filename)) {
35+
byte[] buffer = new byte[1024];
36+
int readBytes;
37+
while ((readBytes = inputStream.read(buffer)) != -1) {
38+
for (int i = 0; i < readBytes; i++) {
39+
System.out.print((char) buffer[i]);
40+
}
41+
}
42+
}
43+
}
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.examplehub.basics.io;
2+
3+
public class FileOutputStreamExample {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.examplehub.basics.io;
2+
3+
import java.io.InputStream;
4+
5+
public class ReadClasspathFile {
6+
public static void main(String[] args) {
7+
InputStream inputStream = ReadClasspathFile.class.getResourceAsStream("default.properties");
8+
System.out.println(inputStream);
9+
}
10+
}
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 static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import org.junit.jupiter.api.Test;
8+
9+
class FileInputStreamExampleTest {
10+
@Test
11+
void testReadFile() throws IOException {
12+
FileInputStreamExample.readFile("pom.xml");
13+
}
14+
15+
@Test
16+
void testReadFileWithTryRecourse() throws FileNotFoundException {
17+
FileInputStreamExample.readFileWithTryRecourse("pom.xml");
18+
}
19+
20+
@Test
21+
void testReadFileWithBuffer() throws IOException {
22+
FileInputStreamExample.readFileWithBuffer("pom.xml");
23+
}
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.examplehub.basics.io;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.examplehub.basics.file.DeleteFile;
6+
import java.io.*;
7+
import java.nio.charset.StandardCharsets;
8+
import org.junit.jupiter.api.Test;
9+
10+
class FileOutputStreamExampleTest {
11+
@Test
12+
void testWriteByteToFile() throws IOException {
13+
try (OutputStream outputStream = new FileOutputStream("temp.txt")) {
14+
outputStream.write('H');
15+
outputStream.write('e');
16+
outputStream.write('l');
17+
outputStream.write('l');
18+
outputStream.write('o');
19+
outputStream.flush();
20+
21+
assertTrue(new File("temp.txt").delete());
22+
}
23+
}
24+
25+
@Test
26+
void testWriteString() throws IOException {
27+
try (OutputStream outputStream = new FileOutputStream("temp.txt")) {
28+
outputStream.write("Hello".getBytes(StandardCharsets.UTF_8));
29+
assertTrue(new File("temp.txt").delete());
30+
}
31+
}
32+
33+
@Test
34+
void testCopyFile() throws IOException {
35+
String srcPath = "pom.xml";
36+
String destPath = "pom.xml.bk";
37+
try (InputStream inputStream = new FileInputStream(srcPath);
38+
OutputStream outputStream = new FileOutputStream(destPath)) {
39+
byte[] bytes = new byte[1024];
40+
int readBytes;
41+
while ((readBytes = inputStream.read(bytes)) != -1) {
42+
outputStream.write(bytes, 0, readBytes);
43+
}
44+
}
45+
46+
assertTrue(DeleteFile.deleteFile("pom.xml.bk"));
47+
}
48+
}

0 commit comments

Comments
 (0)