1. 程式人生 > >Java-IO操作

Java-IO操作

style leo 之間 dmi adl puts 保存數據 關聯 接收

IO流包括字節流(Byte)和字符流(String/char)

字節流:

在JDK中定義了兩個抽象類InputStream和OutputStream,它們是字節流的頂級父親

InputStream的常用方法:

int read(),從輸入流讀取一個8位的字節,把它轉換為0~255之間的整數,並返回這一整數。

int read(byte[] b ),從輸入流讀取若幹個字節,把它們保存到參數b指定的字節數組中,返回的整數表示讀取字節數。

int read(byte[] b ,int off,int len),從輸入流讀取若幹個字節,把它們保存到參數b指定的字節數組中,off指定字節數組開始保存數據的起始下標,len表示讀取的字節數目。

void close() 關閉此輸入流並釋放與該流關聯的所有系統資源。

OutputStream的常用方法:

void write(),像輸出流寫入一個8位的字節。

void write(byte[] b ),參數b指定的字節數組的所有字節寫到輸出流。

void read(byte[] b ,int off,int len),把參數b指定的字節數組中從off指定開始的len個字節寫入輸入流。

void flush(),刷新此輸出流並強制寫出所有緩沖的輸出字節。

void close() 關閉此輸出流並釋放與該流關聯的所有系統資源。

FileInputStream

package stream;

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class FileInputStreamDemo{ public static void main(String[] args) { File srcFile=new File("CopyStreamDemo.txt"); try { InputStream inputStream
=new FileInputStream(srcFile); byte[] buffer=new byte[1024]; int len=-1; while((len=inputStream.read(buffer))!=-1) {//從輸入流inputStream讀取若幹字節,把它們保存到參數buffer中去,同時判斷是否讀到文件末尾 String text=new String(buffer,0, len, "utf-8");//將字節數組轉換為字符串 System.out.printf(text); } inputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

FileOutputStream

 1 package stream;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.OutputStream;
 8 
 9 public class FileOutputStreamDemo {
10 
11     public static void main(String[] args) {
12         File desfile=new File("target.txt");
13         try {
14             String text="我的第一個記事本程序\r\n還有第二行";
15             OutputStream os=new FileOutputStream(desfile);
16             byte[] buffer=text.getBytes("utf-8");//將字符串轉換為字節數組
17             os.write(buffer);
18             System.out.println("文件寫入完成!");
19             os.close();
20         } catch (FileNotFoundException e) {
21             e.printStackTrace();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }
26 }

此外,註意在IO包中提供的兩個帶緩沖的字節流,BufferedInputStream和BufferedOutputStream,它們的構造方法中分別接收InputStream和OutputStream類型的參數作為被包裝的對象。

 1 import java.io.BufferedInputStream;
 2 import java.io.BufferedOutputStream;
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 public class FileCopy{
10     public static void main(String[] args) {
11         File srcFile=new File("CopyStreamDemo.txt");
12         File desFile=new File("target.java");
13         try {
14             /**
15              * 帶緩沖的字節流,BufferedInputStream和BufferedOutputStream,二者接收InputStream和OutputStream作為參數
16              */
17             BufferedInputStream is=new BufferedInputStream(new FileInputStream(srcFile));
18             BufferedOutputStream os=new BufferedOutputStream(new FileOutputStream(desFile));
19             byte[] buffer=new byte[1024];
20             int len=-1;
21             System.out.println("正在復制文件,請稍後...");
22             while((len=is.read(buffer))!=-1) {
23                 os.write(buffer,0,len);
24             } 
25             System.out.println("文件復制完成!");
26             is.close();
27             os.close();
28         } catch (FileNotFoundException e) {
29             e.printStackTrace();
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33     }
34 }

字符流:

字符流也有兩個頂級父親,分別是Reader和Writer,其中Reader是字符輸入流,用於從某個源設備讀取字符,Writer是字符輸出流,用於向某個目標設備寫入字符。

其中,FileReader和FileWriter用於讀寫文件,BufferedReader和BufferedWriter是具有緩沖功能的流,它們可以提高讀寫效率。

FileReader和FileWriter實現文件讀寫:

 1 package chars;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import java.io.Reader;
 9 import java.io.Writer;
10 
11 public class CopyByCharsDemo {
12     public static void main(String[] args) {
13         File srcFile=new File("BufferedReaderDemo.txt");
14         File desFile=new File("dest.txt");
15         try {
16             Reader reader=new FileReader(srcFile);
17             Writer writer=new FileWriter(desFile);
18             char[] buffer=new char[1024];
19             int len=-1;
20             while((len=reader.read(buffer))!=-1) {
21                 writer.write(buffer);
22             }
23             System.out.println("復制完成!");
24             reader.close();
25             writer.close();
26         } catch (FileNotFoundException e) {
27             e.printStackTrace();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31         
32     }
33 }

BufferedReader

 1 package chars;
 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  * 文件Reader,BufferedReader同BufferedInputStream一樣,內部參數是new FileReader(srcFile)
10  * @author Administrator
11  *
12  */
13 public class BufferedReaderDemo {
14     public static void main(String[] args) {
15         File srcFile=new File("CopyByCharsDemo.txt");
16         try {
17             BufferedReader bReader=new BufferedReader(new FileReader(srcFile));
18             String res=null;
19             while((res=bReader.readLine())!=null) {//每次讀取一行文本,判斷是否到文件結尾
20                 System.out.println(res);
21             }
22             bReader.close();
23         } catch (FileNotFoundException e) {
24             e.printStackTrace();
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }
29 
30 }

總結:

1.文件讀取時,不論是用InputStream還是Reader還是BufferedInputStream讀取時都用len,while((len=is.read(buffer))!=-1),但是在BufferedReader中,定義一個String res=null,這時的判斷條件是 while((res=br.readLine())!=null)
2.文件讀寫完成後,記得要善後,即is.close()

Java-IO操作