File類(lèi)
對(duì)文件進(jìn)行創(chuàng)建,刪除,以及訪問(wèn)文件內(nèi)容。
public File(String 路徑文件)設(shè)置要操作文件的路徑
public File(File 父目錄 String 路徑文件)設(shè)置要操作文件的父目錄與子文件路徑
public boolean createNewFile() throws IOException 文件不存在時(shí)創(chuàng)建文件
public boolean delete() 刪除文件
public boolean exists()判斷文件是否存在
File f = new File("D:\\text.txt");
if (f.exists()){
System.out.println("文件純?cè)?,?zhí)行刪除"+f.delete());
}else {
System.out.println("文件不存在,執(zhí)行創(chuàng)建"+f.createNewFile());
}
由于不同平臺(tái)的路徑分隔符不同,所以路徑盡可能用常量File.separator來(lái)編寫(xiě)
File f = new File("D:"+File.separator+"txt");
在子目錄下創(chuàng)建
需要先建立子目錄
public boolean mkdir()創(chuàng)建單級(jí)目錄
public boolean mkdirw() 創(chuàng)建多級(jí)目錄
public String getParent()獲取父路徑的信息
public File getParentFile()獲取父路徑的實(shí)例
File f = new File("D:"+File.separator+"demo"+File.separator+"demo2"+File.separator+"demo.txt");
File parentfile = f.getParentFile(); //獲取父路徑實(shí)例 D:/demo/demo2
if (!parentfile.exists()){//判斷是否存在
parentfile.mkdirs();//不再存在則創(chuàng)建
}
由于文件創(chuàng)建比較慢,一般采用靜態(tài)代碼塊的方式提前創(chuàng)建
private static File f = new File("D:"+File.separator+"demo"+File.separator+"demo2"+File.separator+"demo.txt");
static { File parentfile = f.getParentFile();
if (!parentfile.exists()){
parentfile.mkdirs();
}
}
常用功能
給功能都是根據(jù)實(shí)例化File文件的路徑所決定的
public boolean canExecute() 文件是否能用
public boolean canRead()文件是否能讀
public boolean canWrite()文件是否能寫(xiě)
public String getCanonicalPath()得到文件的絕對(duì)地址
public boolean isDirectory()是否為目錄
public boolean isFile()是否為文件
public long lastModified()得到最后一次修改時(shí)間
public long length()文件長(zhǎng)度
public String getName()文件名字
public String[] list() 獲取該目錄下的子目錄所有信息
public File[] listFiles()獲取該目錄下的子目錄所有路徑信息
public boolean renameTo (File 文件路徑) 給文件更名
OutputStream
字節(jié)輸出流 輸出單個(gè)字節(jié)
該類(lèi)為抽象類(lèi)繼承Closeable接口和Flashable接口
public abstract void write(int b) throws IOException 寫(xiě)入單個(gè)字節(jié)
public void write(byte[] b) throws IOException輸出全部字節(jié)數(shù)組
public void write(byte[] b, int off, int len) throws IOException輸出部分字節(jié)數(shù)組
該類(lèi)的子類(lèi)Fileoutputstream ,byteArraystream..
Fileoutputstream (文件輸出流)
public FileOutputStream(File file) throws FileNotFoundException 設(shè)置文件寫(xiě)入路徑 (每次寫(xiě)入覆蓋)
public FileOutputStream(File file, boolean append) throws FileNotFoundException設(shè)置文件寫(xiě)入路徑 (append 為true每次寫(xiě)入追加)
OutputStream outputStream = new FileOutputStream(f,true);
outputStream.write("hello".getBytes());
outputStream.close();
InputStream
字節(jié)輸入流 輸入單個(gè)字節(jié)
該類(lèi)為抽象類(lèi)繼承Closeable接口
public abstract int read() throws IOException 讀取單個(gè)字節(jié)沒(méi)有返回-1
public int read(byte[] b) throws IOException讀取內(nèi)容到字節(jié)數(shù)組
public int read(byte[] b, int off, int len) throws IOException讀取部分內(nèi)容到字節(jié)數(shù)組
public int available() throws IOException 獲取可用長(zhǎng)度
該類(lèi)的子類(lèi)Fileinputstream 同文件輸出流
Writer
字符輸出流
該類(lèi)為抽象類(lèi)繼承Closeable接口和Flashable接口和Appendable接口
方法同OutputStream類(lèi)
public Writer append(char c) throws IOException追加字符
Reader
字符輸入流
該類(lèi)為抽象類(lèi)繼承Closeable接口Readable接口
方法同intputStream類(lèi)
Outputstream,InputSteam同Writer,Reader的區(qū)別就是一個(gè)是處理字節(jié)一個(gè)是處理字符。
使用字節(jié)比較多,文件傳輸一般使用字節(jié)。字符常用與中文處理,字符流實(shí)則在緩沖區(qū)轉(zhuǎn)化為字節(jié)流來(lái)處理。

