Java
1. File
File:
文件和路徑名的抽象表示竿刁。
File.pathSeparator
表示為";"
與系統(tǒng)相關(guān)的路徑分隔符妒峦。也就是一個(gè);
// 路徑的表示形式
String path = "E:\\xp\\test\\2.jpg";
// 動(dòng)態(tài)表示
path = "E:" +File.separator + "xp" + File.separator + "test" + File.separator + "2.jpg";
// 推薦方式
path = "E:/xp/test/2.jpg";
2.相對(duì)路徑
String parentPath = "E:/xp/test";
String name = "2.jpg";
// 相對(duì)路徑
File src = new File(parentPath,name);
src = new File(new File(parentPath),name);
src.getName(); // 2.jpg
src.getPath(); // E:/xp/test/2.jpg
3.絕對(duì)路徑
// 絕對(duì)路徑
src = new File("E:/xp/test/2.jpg");
src.getName(); // 2.jpg
src.getPath(); // E:/xp/test/2.jpg
4.沒有盤符
src = new File("test.txt");
src.getName(); //test.txt
src.getPath(); //test.txt
src.getAbsolutePath(); // D:\workspace\IO\test.txt
相對(duì)于文件的位置而言
5.File的常用方法
File src = new File("test.txt");
src.getName(); // 返回名稱
src.getPath(); // 如果是絕對(duì)路徑奈籽,返回完整路徑痛悯,否則相對(duì)路徑
src.getAbsolutePath(); // 返回絕對(duì)路徑
src.getParent(); // 返回上一級(jí)目錄鸵鸥,如果是相對(duì)嵌巷,沒有上一級(jí)萄凤,返回null
src.exists(); // 測(cè)試此抽象路徑名表示的文件或目錄是否存在
src.canRead(); // 測(cè)試文件是否可以讀取
src.canWrite(); // 測(cè)試文件是否可以修改
src.isFile(); // 判斷表示的是否是普通文件
src.isDirectory(); // 判斷表示的是否是目錄
src.isAbsolute(); // 判斷是否是絕對(duì)路徑
src.length(); // 文件的長(zhǎng)度,為字節(jié)數(shù)搪哪。
String path = "E:/xp/test/1.jpg";
File file = new File(path);
if (!file.exists()) {
try {
boolean flag = file.createNewFile(); // 當(dāng)文件不存在時(shí)靡努,創(chuàng)建一個(gè)新文件。
System.out.println(flag ? "成功" : "失敗");
} catch (IOException e) {
e.printStackTrace();
}
}
file.delete(); // 刪除一個(gè)文件或目錄,目錄必須為空惑朦,才能刪除
try {
File temp = File.createTempFile("tes",".temp",new File("E:/xp/test/")); // 創(chuàng)建一個(gè)臨時(shí)的空文件兽泄,使用指定的前綴和后綴為文件名
temp.deleteOnExit(); // 退出即刪除
} catch (IOException e) {
e.printStackTrace();
}
6.操作目錄
/**
* 操作目錄
* mkdir(): 創(chuàng)建目錄,必須確保父目錄存在漾月,如果不存在病梢,創(chuàng)建失敗
* mkdirs(): 創(chuàng)建目錄,如果父目錄鏈不存在一同創(chuàng)建
* list(): 文件\目錄字符串形式
*
*/
public static void main() {
String path = "E:/xp/test";
File src = new File(path);
if (src.isDirectory()) { // 為目錄
String[] subNames = src.list();
for (String temp : subNames) {
System.out.println(temp);
}
File[] subFiles = src.listFiles();
for (File temp : subFiles) {
System.out.println(temp.getAbsolutePath());
}
// 添加過(guò)濾器
subFiles = src.listFiles(new FilenameFilter() {
// dir: 自己的src
@Override
public boolean accept(File dir, String name) {
return new File(dir,name).isFile() && name.endsWith(".java");
}
});
}
}
7. IO流
一.概念
流:流動(dòng)栅屏,流向飘千,從一端移動(dòng)到另一端 源頭與目的地
程序 與 文件、數(shù)組栈雳、網(wǎng)絡(luò)連接护奈、數(shù)據(jù)庫(kù),以程序?yàn)橹行?/strong>
二.IO流分類
- 流向:輸入流與輸出流
- 數(shù)據(jù):
- 字節(jié)流:二進(jìn)制哥纫,可以一切文件 包括 純文本 doc霉旗,音頻,視頻等等蛀骇。
- 字符流:文本文件厌秒,只能處理純文本
- 功能:
- 節(jié)點(diǎn):包裹源頭
- 處理:增強(qiáng)功能,提供性能
三.字符流與字節(jié)流
1. 字節(jié)流
輸入流:InputStream
(抽象類)
-
read(byte[] b) :
從輸入流讀取一些字節(jié)數(shù)擅憔,并將它們存儲(chǔ)到緩沖區(qū) b 鸵闪。 -
read(byte[] b, int off, int len):
從輸入流讀取最多 len字節(jié)的數(shù)據(jù)到一個(gè)字節(jié)數(shù)組。 -
close() :
關(guān)閉此輸入流并釋放與流相關(guān)聯(lián)的任何系統(tǒng)資源暑诸。
輸出流:OutputStream
(抽象類)
-
write(byte[] b) :
將b.length
字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流蚌讼。 -
write(byte[] b, int off, int len) :
從指定的字節(jié)數(shù)組寫入len
個(gè)字節(jié),從偏移off
開始輸出到此輸出流个榕。 -
flush() :
刷新此輸出流并強(qiáng)制任何緩沖的輸出字節(jié)被寫出篡石。 -
close() :
關(guān)閉此輸出流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。
2.字符流
輸入流:Reader:
用于讀取字符流的抽象類
-
read(char[] cbuf) :
將字符讀入數(shù)組西采。 -
read(char[] cbuf, int off, int len) :
將字符讀入數(shù)組的一部分凰萨。 -
close() :
關(guān)閉流并釋放與之相關(guān)聯(lián)的任何系統(tǒng)資源
輸出流:Writer:
用于寫入字符流的抽象類
-
write(char[] cbuf) :
寫入一個(gè)字符數(shù)組。 -
write(char[] cbuf, int off, int len) :
寫入字符數(shù)組的一部分械馆。 -
flush() :
刷新流胖眷。 -
close() :
關(guān)閉流,先刷新霹崎。
四.操作
- 舉例:搬家 ---》 讀取文件
1.關(guān)聯(lián)房子 ---》建立與文件聯(lián)系
2.選擇搬家 ---》選擇對(duì)應(yīng)流
3.搬家 ---》讀取|寫入
1.卡車大小 ---》數(shù)組大小
2.運(yùn)輸 ---》讀取瘦材,寫入
4.打發(fā)over ---》釋放資源
- 舉例:搬家 ---》 讀取文件
- 操作
1.建立聯(lián)系
2.選擇流
3.操作:數(shù)組大小read write
4.釋放資源