JAVA 操作檔案

2021-08-06 06:18:59 字數 2009 閱讀 8036

public class demo 

//建立檔案

public static boolean createfile(string filepath) catch (ioexception e)

} return result;

} //建立資料夾

public static boolean createdirectory(string directory)

return result;

} //刪除檔案

public static boolean deletefile(string filepath)

return result;

} //刪除資料夾 遞迴刪除資料夾下面的子檔案和資料夾

public static void deletedirectory(string filepath)

if(file.isdirectory())

}file.delete();

} public static string readfilebybytes(string filepath)

stringbuffer content = new stringbuffer();

try

fileinputstream.close();

} catch (filenotfoundexception e) catch (ioexception e)

return content.tostring();

} //2.以字元為單位讀取檔案,常用於讀文字,數字等型別的檔案,支援中文

public static string readfilebychars(string filepath)

stringbuffer content = new stringbuffer();

char temp = new char[1024];

fileinputstream fileinputstream;

try

fileinputstream.close();

inputstreamreader.close();

} catch (filenotfoundexception e) catch (ioexception e)

return content.tostring();

} //3.以行為單位讀取檔案,常用於讀面向行的格式化檔案

public static listreadfilebylines(string filepath)

listcontent = new arraylist();

tryfileinputstream.close();

inputstreamreader.close();

reader.close();

}catch(filenotfoundexception e)catch(ioexception e)

return content;

} //6.寫檔案,字串寫入檔案的幾個類中,filewriter效率最高,bufferedoutputstream次之,fileoutputstream最差。

//1.通過fileoutstream寫入檔案

public static void writefilebyfileoutputstram(string filepath,string content)throws ioexception

} //2.通過bufferedoutputstream寫入檔案

public static void writefilebybufferedoutputstream(string filepath,string content)throws ioexception

} //3.通過filewriter將字串寫入檔案

public static void writefilebywriter(string filepath,string content)throws ioexception

}}

java操作檔案

一.獲得控制台使用者輸入的資訊 獲得控制台使用者輸入的資訊 return throws ioexception public string getinputmessage throws ioexception.七.建立檔案 資料夾 1.建立資料夾 建立資料夾 param path 目錄 public...

Python IO操作 檔案操作

讀檔案使用python內建的函式open 可以開啟乙個檔案 類似c語言 f open file.txt r f.read f.close 檔案讀寫時可能都會出錯,產生ioerror,一旦出錯f.close 就不能用,所以我們可以使用try finally try f open file.txt r ...

基本操作 檔案操作

開啟檔案 f open yesterday r encoding utf 8 讀寫 f open yesterday w encoding utf 8 寫讀 f open yesterday a encoding utf 8 追加寫讀 f open yesterday wb 寫讀 二進位制檔案 vi...