Java IO流(十)
File類 |
IO流概述 |
節點流(或檔案流) |
緩衝流的使用 |
轉換流的使用 |
其它的流的使用 |
物件流的使用 |
RandomAccessFile的使用 |
Path、Paths、Files的使用 |
File類
File類的理解
1. File類的一個物件,代表一個檔案或一個檔案目錄(俗稱:資料夾)
2. File類宣告在java.io包下
3. File類中涉及到關於檔案或檔案目錄的建立、刪除、重新命名、修改時間、檔案大小等方法,
並未涉及到寫入或讀取檔案內容的操作。如果需要讀取或寫入檔案內容,必須使用IO流來完成。
4. 後續File類的物件常會作為引數傳遞到流的構造器中,指明讀取或寫入的"終點".
File的例項化
常用構造器:
File(String filePath)
File(String parentPath,String childPath)
File(File parentFile,String childPath)
路徑的分類
相對路徑:相較於某個路徑下,指明的路徑。
絕對路徑:包含碟符在內的檔案或檔案目錄的路徑
說明:
IDEA中:
如果大家開發使用JUnit中的單元測試方法測試,相對路徑即為當前Module下。
如果大家使用main()測試,相對路徑即為當前的Project下。
Eclipse中:
不管使用單元測試方法還是使用main()測試,相對路徑都是當前的Project下。
路徑分隔符
windows和DOS系統預設使用“\”來表示
UNIX和URL使用“/”來表示
File類的常用方法
IO流概述
流的分類:
1.操作資料單位:位元組流、字元流
2.資料的流向:輸入流、輸出流
3.流的角色:節點流、處理流
流的體系結構
說明:紅框對應的是IO流中的4個抽象基類。藍框的流需要重點關注。
重點說明的幾個流結構:
輸入、輸出的標準化過程
輸入過程:
① 建立File類的物件,指明讀取的資料的來源。(要求此檔案一定要存在)
② 建立相應的輸入流,將File類的物件作為引數,傳入流的構造器中
③ 具體的讀入過程:
建立相應的byte[] 或 char[]。
④ 關閉流資源
說明:程式中出現的異常需要使用try-catch-finall
輸出過程:
① 建立File類的物件,指明寫出的資料的位置。(不要求此檔案一定要存在)
② 建立相應的輸出流,將File類的物件作為引數,傳入流的構造器中
③ 具體的寫出過程:
write(char[]/byte[] buffer,0,len)
④ 關閉流資源
說明:程式中出現的異常需要使用try-catch-finally處理。
節點流(或檔案流)
FileReader的使用
/
說明點:
1. read()的理解:返回讀入的一個字元。如果達到檔案末尾,返回-1
2. 異常的處理:為了保證流資源一定可以執行關閉操作。需要使用try-catch-finally處理
3. 讀入的檔案一定要存在,否則就會報FileNotFoundException。
/
@Test
public void testFileReader1() {
FileReader fr = null;
try {
//1.File類的例項化
File file = new File("hello.txt");
//2.FileReader流的例項化
fr = new FileReader(file);
//3.讀入的操作
//read(char[] cbuf):返回每次讀入cbuf陣列中的字元的個數。如果達到檔案末尾,返回-1
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
//方式一:
//錯誤的寫法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正確的寫法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//錯誤的寫法,對應著方式一的錯誤的寫法
// String str = new String(cbuf);
// System.out.print(str);
//正確的寫法
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr != null){
//4.資源的關閉
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter的使用
/
從記憶體中寫出資料到硬碟的檔案裡。
說明:
1. 輸出操作,對應的File可以不存在的。並不會報異常
2.
File對應的硬碟中的檔案如果不存在,在輸出的過程中,會自動建立此檔案。
File對應的硬碟中的檔案如果存在:
如果流使用的構造器是:FileWriter(file,false) / FileWriter(file):對原檔案的覆蓋
如果流使用的構造器是:FileWriter(file,true):不會對原檔案覆蓋,而是在原檔案基礎上追加內容
/
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
//1.提供File類的物件,指明寫出到的檔案
File file = new File("hello1.txt");
//2.提供FileWriter的物件,用於資料的寫出
fw = new FileWriter(file,false);
//3.寫出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流資源的關閉
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文字檔案的複製
@Test
public void testFileReaderFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.建立File類的物件,指明讀入和寫出的檔案
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
//不能使用字元流來處理圖片等位元組資料
// File srcFile = new File("愛情與友情.jpg");
// File destFile = new File("愛情與友情1.jpg");
//2.建立輸入流和輸出流的物件
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
//3.資料的讀入和寫出操作
char[] cbuf = new char[5];
int len;//記錄每次讀入到cbuf陣列中的字元的個數
while((len = fr.read(cbuf)) != -1){
//每次寫出len個字元
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.關閉流資源
//方式一:
// try {
// if(fw != null)
// fw.close();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// try {
// if(fr != null)
// fr.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//方式二:
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream / FileOutputStream的使用:
1. 對於文字檔案(.txt,.java,.c,.cpp),使用字元流處理
2. 對於非文字檔案(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用位元組流處理
/
實現對圖片的複製操作
/
@Test
public void testFileInputOutputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//1.造檔案
File srcFile = new File("愛情與友情.jpg");
File destFile = new File("愛情與友情2.jpg");
//2.造流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
//3.複製的過程
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
//4.關閉流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
緩衝流的使用
緩衝流涉及到的類:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
作用:提供流的讀取、寫入的速度
提高讀寫速度的原因:內部提供了一個緩衝區。預設情況下是8kb
使用BufferedInputStream和BufferedOutputStream:處理非文字檔案
//實現檔案複製的方法
public void copyFileWithBuffered(String srcPath,String destPath){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造檔案
File srcFile = new File(srcPath);
File destFile = new File(destPath);
//2.造流
//2.1 造節點流
FileInputStream fis = new FileInputStream((srcFile));
FileOutputStream fos = new FileOutputStream(destFile);
//2.2 造緩衝流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.複製的細節:讀取、寫入
byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.資源關閉
//要求:先關閉外層的流,再關閉內層的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//說明:關閉外層流的同時,內層流也會自動的進行關閉。關於內層流的關閉,我們可以省略.
// fos.close();
// fis.close();
}
}
使用BufferedReader和BufferedWriter:處理文字檔案
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//建立檔案和相應的流
br = new BufferedReader(new FileReader(new File("dbcp.txt")));
bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
//讀寫操作
//方式一:使用char[]陣列
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf)) != -1){
// bw.write(cbuf,0,len);
// // bw.flush();
// }
//方式二:使用String
String data;
while((data = br.readLine()) != null){
//方法一:
// bw.write(data + "\n");//data中不包含換行符
//方法二:
bw.write(data);//data中不包含換行符
bw.newLine();//提供換行的操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關閉資源
if(bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
轉換流的使用
轉換流涉及到的類:屬於字元流
InputStreamReader:將一個位元組的輸入流轉換為字元的輸入流
解碼:位元組、位元組陣列 --->字元陣列、字串
OutputStreamWriter:將一個字元的輸出流轉換為位元組的輸出流
編碼:字元陣列、字串 ---> 位元組、位元組陣列
說明:編碼決定了解碼的方式
作用:提供位元組流與字元流之間的轉換
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("dbcp.txt");
// InputStreamReader isr = new InputStreamReader(fis);//使用系統預設的字符集
//引數2指明瞭字符集,具體使用哪個字符集,取決於檔案dbcp.txt儲存時使用的字符集
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//使用系統預設的字符集
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
isr.close();
}
/*
此時處理異常的話,仍然應該使用try-catch-finally
綜合使用InputStreamReader和OutputStreamWriter
*/
@Test
public void test2() throws Exception {
//1.造檔案、造流
File file1 = new File("dbcp.txt");
File file2 = new File("dbcp_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//2.讀寫過程
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf)) != -1){
osw.write(cbuf,0,len);
}
//3.關閉資源
isr.close();
osw.close();
檔案編碼的方式(比如:GBK),決定了解析時使用的字符集(也只能是GBK)。
常見的編碼表
ASCII:美國標準資訊交換碼。
用一個位元組的7位可以表示。
ISO8859-1:拉丁碼錶。歐洲碼錶
用一個位元組的8位表示。
GB2312:中國的中文編碼表。最多兩個位元組編碼所有字元
GBK:中國的中文編碼表升級,融合了更多的中文文字元號。最多兩個位元組編碼
Unicode:國際標準碼,融合了目前人類使用的所字元。為每個字元分配唯一的字元碼。所有的文字都用兩個位元組來表示。
UTF-8:變長的編碼方式,可用1-4個位元組來表示一個字元。
客戶端/瀏覽器端 <----> 後臺(java,GO,Python,Node.js,php) <----> 資料庫
要求前前後後使用的字符集都要統一:UTF-8.
其它的流的使用
標準的輸入輸出流:
System.in:標準的輸入流,預設從鍵盤輸入
System.out:標準的輸出流,預設從控制檯輸出
修改預設的輸入和輸出行為:
System類的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定輸入和輸出的流。
列印流:
PrintStream 和PrintWriter
說明:
提供了一系列過載的print()和println()方法,用於多種資料型別的輸出
System.out返回的是PrintStream的例項
資料流:
DataInputStream 和 DataOutputStream
作用:用於讀取或寫出基本資料型別的變數或字串
/*
練習:將記憶體中的字串、基本資料型別的變數寫出到檔案中。
注意:處理異常的話,仍然應該使用try-catch-finally.
*/
@Test
public void test3() throws IOException {
//1.
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
//2.
dos.writeUTF("劉建辰");
dos.flush();//重新整理操作,將記憶體中的資料寫入檔案
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
//3.
dos.close();
}
/*
將檔案中儲存的基本資料型別變數和字串讀取到記憶體中,儲存在變數中。
注意點:讀取不同型別的資料的順序要與當初寫入檔案時,儲存的資料的順序一致!
*/
@Test
public void test4() throws IOException {
//1.
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
//2.
String name = dis.readUTF();
int age = dis.readInt();
boolean isMale = dis.readBoolean();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("isMale = " + isMale);
//3.
dis.close();
}
物件流的使用
物件流:ObjectInputStream 和 ObjectOutputStream
作用:
ObjectOutputStream:記憶體中的物件--->儲存中的檔案、通過網路傳輸出去:序列化過程
ObjectInputStream:儲存中的檔案、通過網路接收過來 --->記憶體中的物件:反序列化過程
物件的序列化機制:
物件序列化機制允許把記憶體中的Java物件轉換成平臺無關的二進位制流,從而允許把這種二進位制流持久地儲存在磁碟
上,或通過網路將這種二進位制流傳輸到另一個網路節點。//當其它程式獲取了這種二進位制流,就可以恢復成原來的Java物件
實現序列化的物件所屬的類需要滿足:
1.需要實現介面:Serializable
2.當前類提供一個全域性常量:serialVersionUID
3.除了當前Person類需要實現Serializable介面之外,還必須保證其內部所屬性也必須是可序列化的。
(預設情況下,基本資料型別可序列化)
補充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修飾的成員變數
序列化程式碼實現:
@Test
public void testObjectOutputStream(){
ObjectOutputStream oos = null;
try {
//1.
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
//2.
oos.writeObject(new String("我愛北京天安門"));
oos.flush();//重新整理操作
oos.writeObject(new Person("王銘",23));
oos.flush();
oos.writeObject(new Person("張學良",23,1001,new Account(5000)));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos != null){
//3.
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
反序列化程式碼實現:
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
Person p1 = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
System.out.println(p1);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
RandomAccessFile的使用
隨機存取檔案流:RandomAccessFile
1.RandomAccessFile直接繼承於java.lang.Object類,實現了DataInput和DataOutput介面
2.RandomAccessFile既可以作為一個輸入流,又可以作為一個輸出流
3.如果RandomAccessFile作為輸出流時,寫出到的檔案如果不存在,則在執行過程中自動建立。
如果寫出到的檔案存在,則會對原檔案內容進行覆蓋。(預設情況下,從頭覆蓋)
4. 可以通過相關的操作,實現RandomAccessFile“插入”資料的效果。seek(int pos)
@Test public void test1() { RandomAccessFile raf1 = null; RandomAccessFile raf2 = null; try { //1. raf1 = new RandomAccessFile(new File("愛情與友情.jpg"),"r"); raf2 = new RandomAccessFile(new File("愛情與友情1.jpg"),"rw"); //2. byte[] buffer = new byte[1024]; int len; while((len = raf1.read(buffer)) != -1){ raf2.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //3. if(raf1 != null){ try { raf1.close(); } catch (IOException e) { e.printStackTrace(); } } if(raf2 != null){ try { raf2.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/*
使用RandomAccessFile實現資料的插入效果
*/
@Test
public void test3() throws IOException {
RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
raf1.seek(3);//將指標調到角標為3的位置
//儲存指標3後面的所資料到StringBuilder中
StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
byte[] buffer = new byte[20];
int len;
while((len = raf1.read(buffer)) != -1){
builder.append(new String(buffer,0,len)) ;
}
//調回指標,寫入“xyz”
raf1.seek(3);
raf1.write("xyz".getBytes());
//將StringBuilder中的資料寫入到檔案中
raf1.write(builder.toString().getBytes());
raf1.close();
//思考:將StringBuilder替換為ByteArrayOutputStream
}
Path、Paths、Files的使用
NIO的使用說明:
Java NIO (New IO,Non-Blocking IO)是從Java 1.4版本開始引入的一套新的IO API,可以替代標準的JavaIO AP。
NIO與原來的IO同樣的作用和目的,但是使用的方式完全不同,NIO支援面向緩衝區的(IO是面向流的)、基於通道的IO操作。
NIO將以更加高效的方式進行檔案的讀寫操作。
隨著 JDK 7 的釋出,Java對NIO進行了極大的擴充套件,增強了對檔案處理和檔案系統特性的支援,以至於我們稱他們為 NIO.2。
Path的使用 ---jdk7提供
Path的說明:Path替換原有的File類。
例項化:
常用方法:
Files工具類 ---jdk7提供
作用:操作檔案或檔案目錄的工具類
常用方法: