java IO流的基本常用操作

1、将文件内容读取至控制台输出

字節流

 1 package com.sth.test;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 
 8 public class Test2 {
 9     public void readFile(String path) {
10         try {
11             File file = new File(path);
12             if (!file.exists()) {
13                 file.createNewFile();
14             }
15             FileInputStream fs = new FileInputStream(file);
16             int m = 0;
17             while ((m = fs.read()) != -1) {
18                 System.out.print((char) m);
19             }
20             System.out.print("\n");
21             fs.close();
22         } catch (FileNotFoundException e) {
23             System.out.println("无文件");
24             e.printStackTrace();
25         } catch (IOException e) {
26             System.out.println("文件讀取錯誤");
27             e.printStackTrace();
28         }
29     }
30 }

或者

 1 package com.sth.test;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 
 8 public class TestReadFile {
 9     public void readFile(String path){
10     try{
11         File file = new File(path);
12         if(!file.exists()){
13             file.createNewFile();
14         }
15         FileInputStream fs = new FileInputStream(file);
16         byte b[] = new byte[1024];
17         int m = 0;
18         while((m=fs.read(b))!=-1){
19             System.out.println(new String(b,0,m));
20         }
21         fs.close();
22     }catch(FileNotFoundException e){
23         System.out.println("无文件");
24         e.printStackTrace();
25     }catch(IOException e){
26         System.out.println("文件读取错误");
27     }
28     }
29 }

字符流

 1 package com.sth.test;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 import java.io.Reader;
 8 
 9 public class ReadByChar {
10     public void readFile(String path){
11         try{
12             File file = new File(path);
13             Reader reader = new FileReader(file);
14             char[] b = new char[1024];
15             int m = 0 ;
16             while((m=reader.read(b))!=-1){
17                 System.out.println(new String(b,0,m));
18             }
19             reader.close();
20         }catch(FileNotFoundException e){
21             System.out.println("文件不存在");
22             e.printStackTrace();
23         } catch(IOException e){
24             System.out.println("文件讀取錯誤");
25             e.printStackTrace();
26         }
27     }
28 }

或者使用節點流和處理流來讀取文件

 1 package com.sth.test;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileReader;
 7 import java.io.IOException;
 8 
 9 public class Jiedianliu {
10     public void readFileBufferedReader(String path) {
11         try {
12             File file = new File(path);
13             FileReader fr = new FileReader(file);
14             BufferedReader br = new BufferedReader(fr);
15             String str = null;
16             while ((str = br.readLine()) != null) {
17                 System.out.println(str);
18             }
19             fr.close();
20             br.close();
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26     }
27 }

 

posted @ 2013-01-20 23:18  そんさん  阅读(684)  评论(0)    收藏  举报