Skip to content

Commit 6a66813

Browse files
committed
add Rule9
1 parent e04aea1 commit 6a66813

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.yiyun.Rule09;
2+
3+
import java.io.*;
4+
5+
public class TestFile implements AutoCloseable {
6+
static String firstLineOfFile(String path) throws IOException {
7+
BufferedReader br = new BufferedReader(new FileReader(path));
8+
try {
9+
return br.readLine();
10+
} finally {
11+
br.close();
12+
}
13+
}
14+
15+
static void copy(String src, String dst) throws IOException {
16+
InputStream in = new FileInputStream(src);
17+
try {
18+
OutputStream out = new FileOutputStream(dst);
19+
try {
20+
byte[] buf = new byte[1024];
21+
int n;
22+
while ((n = in.read(buf)) >= 0)
23+
out.write(buf, 0, n);
24+
} finally {
25+
out.close();
26+
}
27+
} finally {
28+
in.close();
29+
}
30+
}
31+
32+
static String firstLineOfFile1(String path) throws IOException {
33+
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
34+
return br.readLine();
35+
}
36+
}
37+
38+
@Override
39+
public void close() throws Exception {
40+
41+
}
42+
}

0 commit comments

Comments
 (0)