JAVA进行基础的文件IO读写

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 public class TestIO {
 8     
 9     public void testRead(String path){
10         FileInputStream is = null;
11         File file = new File(path);
12         try {
13             is = new FileInputStream(file);
14             byte[] byteBuffer = new byte[is.available()];
15             int read = 0;
16             while((read = is.read(byteBuffer))!= -1){
17                 System.out.write(byteBuffer, 0, read);
18             }
19         } catch (FileNotFoundException e) {
20             e.printStackTrace();
21         } catch (IOException e) {
22             e.printStackTrace();
23         }finally{
24             try {
25                 if(is!=null){
26                     is.close();
27                 }
28             } catch (IOException e) {
29                 e.printStackTrace();
30             }
31         }
32     }
33     
34     public void testWrite(String content, String path){
35         File file = new File(path);
36         try {
37             FileOutputStream os = new FileOutputStream(file);
38             byte[] byteBuffer = content.getBytes();
39             os.write(byteBuffer, 0, byteBuffer.length);
40             os.flush();
41             os.close();
42         } catch (FileNotFoundException e) {
43             e.printStackTrace();
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47     }
48     
49     public static void main(String[] args) {
50         TestIO io = new TestIO();
51         io.testRead("src/test.txt");
52         io.testWrite("this is a test", "src/test.txt");
53         //io.testRead("src/test.txt");
54     }
55     
56 }

 

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
public class TestIO {public void testRead(String path){FileInputStream is = null;File file = new File(path);try {is = new FileInputStream(file);byte[] byteBuffer = new byte[is.available()];int read = 0;while((read = is.read(byteBuffer))!= -1){System.out.write(byteBuffer, 0, read);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}}public void testWrite(String content, String path){File file = new File(path);try {FileOutputStream os = new FileOutputStream(file);byte[] byteBuffer = content.getBytes();os.write(byteBuffer, 0, byteBuffer.length);os.flush();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {TestIO io = new TestIO();io.testRead("src/test.txt");io.testWrite("this is a test", "src/test.txt");//io.testRead("src/test.txt");}}

 

posted @ 2016-02-16 19:02  赵吃饭  阅读(265)  评论(0编辑  收藏  举报