File tree Expand file tree Collapse file tree
effective_java/src/main/java/com/yiyun/Rule09 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments