1 IO流的概念
流是一個抽象的概念,當程式讀取資料時,就會開啟一個通往資料源的流,這個資料源可以是檔案、記憶體或者是網絡連結;或當程式寫入資料時,就會開啟一個通往目的地的流
java中把不同到的來源或者目的地的資料都統一抽象為資料流
流的本質:就是對資料的傳輸,資料的一個集合,有方向性
2 IO流體系結構
- IO流的體系結構圖
- 流的實作類
3Java中流的分
3.1按流向分
輸入流:從 資料源 (磁盤,U盤,網絡,鍵盤等)讀取資料到 程式 的流
輸出流:從 程式 寫入資料到 目的地資料源(磁盤,U盤,網絡,顯示器等) 的流
3.2按資料傳輸機關分
位元組流:以位元組為機關傳輸資料的流
字元流:以字元為機關傳輸資料的流
3.3按功能分
節點流:用于直接操作目标裝置的流
過濾流:對一個已存在的流進行包裝,以提供更強大和靈活的讀寫功能
4File類介紹
4.1File類的作用
對檔案,目錄的抽象表示,該類主要對目錄和檔案進行建立、修改、删除、過濾等操作
4.2常見的構造函數
//3種構造方法
File(String pathname) //用路徑字元串執行個體化File執行個體
File(String parent, String child) //從父路徑名(字元串)和子路徑名(字元串)來建構file執行個體
File(File parent, String child) //根據父路徑File執行個體和子路徑名(字元串)來建構執行個體
//執行個體化示範
//String pathname
File file1 = new File("/Users/ccc/Desktop/IO/file1.txt");
//String parent, String child
File file2 = new File("/Users/ccc/Desktop/IO", "file2.txt");
//File parent, String child
File parent = new File("/Users/ccc/Desktop/IO");//先執行個體化出父路徑
File file3 = new File(parent, "file3.txt");
4.3提供方法
4.3.1建立和删除
//建立檔案
File test = new File("/Users/ccc/Desktop/IO/a.txt");
try {
//public boolean createNewFile() throws IOException
// 建立新檔案 當檔案不存在時建立該檔案并傳回true,當檔案存在時不會建立檔案并傳回false
//建立檔案時,路徑必須存在,否則建立失敗
boolean newFile = test.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//沒有指明建立路徑時,檔案将會被建立在這個項目路徑下
File test2 = new File("b.txt");
test2.createNewFile();
//建立目錄
//boolean mkdir()
// 通過抽象路徑來建立目錄,mkdir()隻能建立一級目錄
File test3 = new File("/Users/ccc/Desktop/IO/c");
test3.mkdir();
//boolean mkdirs()
//mkdirs()用來建立多級目錄
File test4 = new File("/Users/ccc/Desktop/IO/d/e");
test4.mkdirs();
//沒有指明建立路徑時,調用生成檔案方法時,檔案将會被建立在這個項目路徑下
File test5 = new File("b");
test5.createNewFile();
//沒有指明建立路徑時,調用生成目錄(檔案夾)方法時,目錄(檔案夾)将會被建立在這個項目路徑下
//test5.mkdirs();
//删除
//删除目錄和檔案
//boolean delete()
//隻能删除存在的一層檔案或者目錄,不能直接删除多級目錄
test5.delete();
4.3.2檔案擷取
//檔案擷取
File file = new File("D:\\ccc\\JAVASE");
//擷取絕對路徑 帶盤符
//String getAbsolutePath()
System.out.println(file.getAbsoluteFile());
//相對路徑 不帶盤符
//String getPath()
System.out.println(file.getPath());
//擷取檔案名或者路徑名
//String getName()
System.out.println(file.getName());
//檔案位元組數
//long length()
System.out.println(file.length());
//擷取目錄下面的内容(檔案或者目錄)
//String[] list()
String[] list = file.list();
for(String i:list ){
System.out.println(i);
}
//傳回目錄檔案和目錄名稱file執行個體數組
//File[] listFiles()
File[] files = file.listFiles();
for(File i: files){
System.out.println(i.getName());
}
4.3.3重命名和修改路徑功能
//重命名功能
//boolean renameTo(File dest)
//先在該項目路徑下建立一個檔案b.txt
File text = new File("b.txt");
//執行個體化一個修改後的名稱bb.txt
File text1 = new File("bb.txt");
text.renameTo(text1);
//修改路徑功能
//boolean renameTo(File dest)
File text = new File("bb.txt");
File text1 = new File("C:\\Users\\ccc\\Desktop\\IO流\\bb.txt");
text.renameTo(text1);
//重命名功能 和 修改路徑功能 都使用renameTo()方法
//差別:修改前後的路徑相同就是重命名,不同則是修改路徑
4.3.4判斷功能
//判斷功能
File test = new File("bb.txt");
test.createNewFile();
//boolean isFile()
//是否是檔案
boolean b = test.isFile();
System.out.println(b);
//boolean isDirectory()
//是否是目錄(檔案夾)
boolean b1 = test.isDirectory();
System.out.println(b1);
//boolean isHidden()
//是否是隐藏檔案
System.out.println(test.isHidden());
//boolean canRead()
//檔案是否可讀
System.out.println(test.canRead());
//boolean canWrite()
//檔案是否可寫
System.out.println(test.canWrite());
//boolean exists()
//檔案是否存在
System.out.println(test.exists());
4.3.5過濾功能的接口方法
public interface FilenameFilter {
//參數為目錄和指定過濾名稱
//傳回結果 true: 可以傳回 false:不能傳回
boolean accept(File dir, String name);
}
5RandomAccessFile(随機通路檔案類)的介紹
5.1聲明形式
5.2構造函數
public RandomAccessFile(String name, String mode)
public RandomAccessFile(File file, String mode) //mode 可以是 r rw rws rwd
r:以隻讀的模式打開,如果調用write方法将會抛出IO異常
rw:以讀和寫的模式打開
rws:以讀和寫的模式打開,要求對”檔案的内容“和”中繼資料“的每個更新都同步到儲存設備
rwd:以讀和寫的模式打開,要求對”檔案的内容“的每個更新都同步到儲存設備
5.3特殊方法
long getFilePointer() //傳回檔案記錄中指針的目前位置
void seek(long pos) //将檔案記錄移動到指定的pos檔案
在檔案中的指定位置插入字元串問題(戳下面藍色字跳轉哦~):
在檔案中的指定位置插入字元串問題