<small>
文件 目錄(信息)
文件目錄信息類
new File(String pathName)
此構(gòu)造絕對不是創(chuàng)建文件卡睦,僅僅表示根據(jù)抽象路徑pathName践险,創(chuàng)建一個File文件實例
File實例肢藐,并不能說明文件就真實存在故河,只是用來判斷文件目錄是否真實存在
boolean createNewFile()
創(chuàng)建文件并返回是否創(chuàng)建成功
boolean delete()
刪除文件
boolean isFile()
判斷是否為文件
boolean isDirectory()
判斷是否為目錄
程序?qū)嵗?/p>
String pathName = "E:\\ksxx\\所有知識點講解\\21\\demo.txt";
//創(chuàng)建一個File實例
File file = new File(pathName);
boolean exists()
*/
boolean isExists = file.exists();
System.out.println("文件是否存在:"+isExists);
if(!isExists){
boolean isCreateFileOk =
file.createNewFile();
System.out.println(isCreateFileOk?"創(chuàng)建文件成功":"創(chuàng)建文件失敗");
}else{
boolean isDeleteFileOk = file.delete();
System.out.println(isDeleteFileOk?"刪除文件成功":"刪除文件失敗");
}
pathName = "E:\\";
file = new File(pathName);
System.out.println("E:\\存在:"+file.exists());
boolean isFile = file.isFile();
System.out.println(isFile?"E:\\是文件":"E:\\不是文件");
boolean isDirectory = file.isDirectory();
System.out.println(isDirectory?"E:\\是目錄":"E:\\不是目錄");
file = new File("/");
System.out.println(file.isDirectory()?"/是目錄":"/不是目錄");
pathName = "E:"+File.separator+"ksxx"+File.separator+
"所有知識點講解"+File.separator+
"21"+File.separator+"demo.txt";
System.out.println(pathName);
pathName = "../demo02.txt";
file = new File(pathName);
file.createNewFile();//創(chuàng)建當(dāng)前目錄下的demo01.t
1、windows與linux的區(qū)別:
* widows中吆豹,目錄的表示為
* 沒有根目錄,但有盤符痘煤,
* 如:C:\、D:\衷快、E:\、F:\养匈、U盤等
* linux中都伪,目錄的表示為 /
* 沒有盤符概念,但有且只有一個根目錄陨晶。
* 所有的路徑都是從根目錄出發(fā)。
* U盤被視為掛載點湿刽。
*
* 不管是什么系統(tǒng)的服務(wù)器褐耳,
* 可以利用File.separator來實現(xiàn)目錄的自動辨析。
* 當(dāng)遇到是windows系統(tǒng)服務(wù)器铃芦,就會自動填充
* 當(dāng)遇到是linux系統(tǒng)服務(wù)器,就會自動填充 /
/*
* 2仁烹、/咧虎、./與../的區(qū)別
* 如果后面不跟內(nèi)容,點后面的/可以省略(建議不要省略)
*
* / 根路徑
* ./(可以直接省略) 用來表示當(dāng)前目錄下
* [./]../ 用來表示上級目錄
*
* 絕對路徑:
* 以 / 開頭的路徑砰诵。
*
* 相對路徑:
* 以./貨../ 開頭的路徑。
long length()
獲得文件的大凶芎(字節(jié)量)
lastModified()
獲得文件最后一次修改時間
public class FileDemo02 {
public static void main(String[] args) throws IOException {
/*
* File:文件目錄信息類
* 查詢文件信息方法
*/
String pathName =
"E:\\ksxx\\所有知識點講解\\21\\demo.txt";
//創(chuàng)建一個File實例
File file = new File(pathName);
/*
* 6尉间、獲得文件的大小(字節(jié)量)
* long length()
*/
long bytes = file.length();
System.out.println("demo.txt文件大姓艹啊:"+bytes);
/*
* 7、獲得文件的最后一次修改時間
*/
long lastModifiedTime = file.lastModified();
System.out.println("demo01.txt最后一次修改時間:"+new Date(lastModifiedTime));
System.out.println("是否為可讀文件:"+file.canRead());
System.out.println("是否為可寫文件:"+file.canWrite());
System.out.println("是否為可執(zhí)行文件:"+file.canExecute());
System.out.println("是否為隱藏文件:"+file.isHidden());
System.out.println("獲得文件名:"+file.getName());
System.out.println("獲得目錄名:"+file.getParent());
System.out.println("獲得抽象路徑:"+file.getPath());
boolean midir();
只能創(chuàng)建單級目錄画切,只有上級目錄存在的前提下,才能創(chuàng)建單級目錄霍弹。如果多級目錄不存在,則無法創(chuàng)建典格。
boolean mkdirs
可以創(chuàng)建多級目錄
RandomAcessFile
文件讀寫操作類:可以通過File實例對文件實現(xiàn)讀寫操作。
RandomAccessFile的構(gòu)造及注意事項
* RandomAccessFile(String name,String mode)
* RandomAccessFile(File file,String mode)
*
* 構(gòu)造二砾肺,其實是對構(gòu)造一的封裝防嗡。
* RandomAccessFile(new File(name),String mode)
*
* 構(gòu)造參數(shù):
* 第一個參數(shù):字符串類型的抽象路徑
* 也可以是抽象路徑封裝好的File實例。
*
* 第二個參數(shù):讀寫模式
* 讀:r (read)
* 寫:w (write)
*
* RandomAccessFile對象創(chuàng)建的
* 注意事項:
* 1裙盾、文件路徑(文件所在的目錄)非真實存在他嫡,
* 會拋出異常:FileNotFoundException
* 2、文件路徑(文件所在的目錄)真實存在涮瞻,
* 只不過是文件不存在:
* ①、只讀模式近顷,必須要求文件存在宁否,
* 否則拋出FileNotFoundException
* ②、讀寫模式慕匠,文件不存在台谊,
* 會自動創(chuàng)建文件。
RandomAccessFile(String name,String mode)
RandomAccessFile(File file,String mode)
String pathName = "./demo01.txt";
RandomAccessFile raf =
new RandomAccessFile(pathName,"rw");
//或者
File file = new File(pathName);
raf = new RandomAccessFile(file,"rw");
文件的讀寫
單個字節(jié)的讀寫
String pathName = "./demo01.txt";
RandomAccessFile raf =
new RandomAccessFile(pathName,"rw");
raf.write('a');//a
raf.write(353);//其實寫進(jìn)去還是a
raf.write(865);//其實寫進(jìn)去還是a
int read1 = raf.read();
// System.out.println(read1);//-1表示讀取到文件末尾
raf.close();//讀寫完畢后锅铅,需要關(guān)閉io通道在一定意義上,就是Ctrl+S保存
raf = new RandomAccessFile(pathName,"rw");
int read1 = raf.read();
System.out.println(read1);//97
int read2 = raf.read();
System.out.println(read2);//97
int read3 = raf.read();
System.out.println(read3);//97
int read4 = raf.read();
System.out.println(read4);//-1
raf.close();
文件的復(fù)制
單字節(jié)復(fù)制
RandomAccessFile file =
new RandomAccessFile("16.rar","rw");
RandomAccessFile copyFile =
new RandomAccessFile("16_copy.rar","rw");
long startTime = System.currentTimeMillis();
/*
* 要對文件實現(xiàn)單字節(jié)的讀寫操作玩荠,
* 必須使用循環(huán)來完成。
*/
int read = 0;
//對file進(jìn)行單字節(jié)讀取操作
while((read = file.read()) != -1){
//讀一個字節(jié)
//對copyFile進(jìn)行單字節(jié)寫操作
copyFile.write(read);//寫
}
//讀寫完畢闷尿,要關(guān)流
file.close();
copyFile.close();
long endTime = System.currentTimeMillis();
//969128ms
System.out.println("本次拷貝女坑,用時:"+(endTime - startTime)+"ms");
字節(jié)數(shù)組的復(fù)制
public static void main(String[] args) throws IOException {
/*
* 通過RandomAccessFile批量字節(jié)的讀寫操作
* 來完成對文件16.rar的拷貝。
* 目標(biāo)文件為16_copy.rar
*
* 實現(xiàn)步驟:
* 先對16.rar文件進(jìn)行批量字節(jié)讀取灌旧。
* 讀取的字節(jié)绰筛,
* 向16_copy.rar中寫描融。
*/
RandomAccessFile file =
new RandomAccessFile("16.rar","rw");
RandomAccessFile copyFile =
new RandomAccessFile("16_copy2.rar","rw");
long startTime = System.currentTimeMillis();
/*
* 要對文件實現(xiàn)單字節(jié)的讀寫操作,
* 必須使用循環(huán)來完成窿克。
*/
int read = 0;
byte[] buf = new byte[1024];
//對file進(jìn)行字節(jié)數(shù)組讀取操作
while((read = file.read(buf)) != -1){
//讀字節(jié)數(shù)組
//對copyFile進(jìn)行字節(jié)數(shù)組寫操作
copyFile.write(buf);//寫
}
//讀寫完畢年叮,要關(guān)流
file.close();
copyFile.close();
long endTime = System.currentTimeMillis();
//1158ms
System.out.println("本次拷貝,用時:"+(endTime - startTime)+"ms");
字節(jié)數(shù)組的復(fù)制效率高很多
總結(jié):
file只是文件目錄的信息類只损,根據(jù)抽象路徑,創(chuàng)建file實例叮叹,通file實例爆存,可以查詢文件,目錄的相關(guān)信息
file不代表文件本身先较,并不能對文件實現(xiàn)讀寫操作