一,File類:文件的創建和刪除 1.File(String pathname):pathname是指路徑名稱。用法 File file = new File("d:/1.txt "); 2.File(String parent, String child):parent是父路徑字元串,child是 ...
一,File類:文件的創建和刪除
1.File(String pathname):pathname是指路徑名稱。用法 File file = new File("d:/1.txt ");
2.File(String parent, String child):parent是父路徑字元串,child是子路徑字元串。用法:File file = new File(" D:/", " letter.txt");
3.File(File f, String child):File file = new file();
如果當前目錄里不存在名為word的文件,File類對象可以調用createNewFile()方法創建一個名為word的新文件。
還可以用文件對象的delete()方法刪除掉文件。
public class FileTest { public static void main(String[] args) { File file = new File("weord.txt"); //創建文件對象,在當前目錄 if(file.exists()) { file.delete(); System.out.println("文件已刪除"); }else{ try{ //try語句塊捕捉可能出現的異常 file.createNewFile(); //在當前目錄下創建文件 System.out.println("文件已創建"); }catch(Exception e) { e.printStackTrace(); // } } } }
二,獲取文件信息:都用特定方法獲取文件的各類信息。
getName():獲取文件的名稱 length():獲取文件的長度 getAbsolutePath():獲取文件的絕對路徑 getParent():獲取文件的父路徑 lastModified():獲取文件最後修改時間
canread():判斷是否可讀 canwrite():判斷是否可寫 exits():判斷是否存在 isFile():判斷是否存在 isDirectory():判斷是否是一個目錄 isHidden():判斷是否是隱藏文件
我們把上面創建和刪除的例子稍微改一下:
public class FileTest { public static void main(String[] args) { File file = new File("word.txt"); //創建文件對象,在當前目錄 if(file.exists()) { //獲取各類信息 String name = file.getName(); System.out.println("文件名稱是:"+ name); long length = file.length(); System.out.println("文件長度是:"+ length); boolean hidden = file.isHidden(); System.out.println("文件是隱藏文件嗎?"+ hidden); String path = file.getAbsolutePath(); System.out.println("文件的絕對路徑是:"+ path); boolean read = file.canRead(); System.out.println("文件是否可讀?"+ read); }else{ try{ //try語句塊捕捉可能的異常 file.createNewFile(); //創建文件 System.out.println("文件已創建"); }catch(Exception e) { e.printStackTrace(); // } } } }
一,文件輸入/輸出流 :FileInputStream和FileOutputStream類
程式運行期間的大部分數據都在記憶體中進行操作,當程式結局了或者關閉後,這些數據都會消失。那麼能不能把這些數據都永久的保存下來呢?
答案是肯定的:用文件輸入/輸出流與指定的文件建立連接,把數據永久性的保存到文件里。
FileInputStream和FileOutputStream類都用來操作磁碟文件。前者具有基本的文件寫入能力,後者有基本的文件讀取能力。
FileInputStream有兩種構造方法:
1.FileInputStream(String name):使用給定的文件名name創建一個FileInputStream對象。
2.FileInputStream(File file):這個構造方法允許在把文件連接輸入流之前對文件做進一步分析。
FileOutputStream類有前者相同的參數構造方法。註意到是:創建一個FileInputStream對象時,可以指定不存在的文件名,但此文件不能是一個已經被其他程式打開的文件。
註意:FileInputStream類和FileOutputStream類都只提供了對位元組或位元組數組的讀取方法。漢字在文件中占用兩個位元組,所以讀取不好會出現亂碼。(所以下麵提到的字元流比位元組流更為簡便)
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class FileTest { public static void main(String[] args) { File file = new File("word.txt"); //創建文件對象,在當前目錄 try { FileOutputStream out = new FileOutputStream(file);//創建FileOutputStream類對象 byte buy[] = "我有java課,在每周一的一二節。".getBytes();//創建數組存放字元串的二進位 out.write(buy); //把數組中的信息寫入到文件里 out.close(); //把流關閉 }catch(Exception e){ e.printStackTrace(); } try { FileInputStream in = new FileInputStream(file);//創建FileIntputStream類對象 byte byt[] = new byte[1024]; //創建數組,定義記憶體大小 int len = in.read(byt); //獲取寫入信息的長度 System.out.println("文件中的信息是:"+ new String(byt, 0, len));//把文件中的信息輸出 in.close(); //關閉流 }catch(Exception e){ e.printStackTrace(); } } }
雖然java在程式結束時自動關閉所有打開的流,但是當使用完流後,顯示地(主動的)關閉所有打開的流是個好習慣。
二,FileReader和FileWriter類(字元流)
FileReader和FileWriter類 對應了FileInputStream和FileOutputStream類。FileReader流順序地讀取文件,只要不關閉流,每次調用read()方法就順序地讀取源中其餘內容,直到源的末尾或流被關閉。
三,帶緩存的輸入/輸出流:BufferInputStream和BufferOutputStream
緩存是I/O的一種性能優化。緩存流為I/O流增加了記憶體緩存區。有了緩存區,使得在流上執行skip(), mark(), reset()等方法。
BufferInputStream類可以對所有InputStream類進行帶緩存區的包裝以達到性能的優化。有兩個構造方法:
1.BufferInputStream(InputStream in):創建一個帶有32個位元組的緩存區。
2.BufferInputStream(InputStream in, int size):按指定的大小創建緩存區。
從構造方法能看出來:BufferInputStream對象位於InputStream類對象之前。 所以位元組數據讀取文件的過程是:文件 --> InputStream --> BufferInputStream --> 目的地
一個最優的緩存區的大小取決於所在的操作系統,可用的記憶體空間,機器配置。
使用BufferOutputStream輸出信息和OutputStream輸出信息完全一樣,不過BufferOutputStream有一個flush()方法來把緩存區的信息強制輸出完。
1.BufferInputStream(InputStream in):創建一個帶有32個位元組的緩存區。
2.BufferInputStream(InputStream in, int size):按指定的大小創建緩存區。
2019-03-24 未完, 過兩天補。