java基础的文件操作(File类)
生活随笔
收集整理的這篇文章主要介紹了
java基础的文件操作(File类)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
1.文件的創建,重命名,刪除
public?class?HelloFile?{public?static?void?main(String[]?args)?{File?file?=?new?File("bin/hello.txt");//絕對路徑,創建在項目內部//判斷文件是否存在if?(file.exists())?{// file.delete();
// System.out.println("文件刪除成功");//文件System.out.println(file.isFile());//輸出判斷是否為文件//路徑(文件夾)System.out.println(file.isDirectory());//輸出判斷是否為文件夾//處于不同的分區,需要使用文件的拷貝,而不是重命名File?nameto?=?new?File("src/new?Hello.txt");file.renameTo(nameto);//運行的重命名的方法 }?else?{System.out.println("文件不存在");//創建文件try?{file.createNewFile();System.out.println("文件已經被創建");}?catch?(IOException?e)?{System.out.println("文件無法被創建");}}} 2.文件夾的創建,重命名,刪除
?文件夾的創建,如果多層路徑不存在的話,就用?mkdirsfolder.mkdirs(),存在的話可以用mkdir() public?class?HelloFolder?{public?static?void?main(String[]?args)?{File?folder?=?new?File("Hello?new?Folder/three");/***?刪除文件夾,只能刪除空的文件夾*/if?(folder.delete())?{System.out.println("done");}?else?{System.out.println("file");}/***?文件的重命名移動,文件夾處于同一個分區中,移動文件夾*/ // File?newfolder?=?new?File("Hello?new?Folder/three"); // if?(folder.renameTo(newfolder))?{ // System.out.println("done"); // }?else?{ // System.out.println("file"); // }/***?文件夾的創建,如果多層路徑不存在的話,就用?mkdirsfolder.mkdirs()*/ // if?(folder.mkdir())?{?//創建文件夾,要求路徑要存在,不存在的話要用?mkdirsfolder.mkdirs(); // System.out.println("文件夾創建成功"); // }?else?{ // if?(folder.exists())?{//判斷文件夾是否存在 // System.out.println("文件夾已經存在,不用創建"); // }?else?{ // System.out.println("文件夾創建失敗"); // } // }; // // }}3.文件屬性的讀取
public?class?ReadFileProperty?{/***?讀取文件屬性*/public?static?void?main(String[]?args){File?file?=?new?File("text.txt");//判斷文件是否存System.out.println("判斷文件是否存在:"+file.exists());//讀取文件的名稱System.out.println("讀取文件是否存在:"+file.getName());//讀取文件的路徑System.out.println("讀取文件的路徑:"+file.getPath());//讀取絕對路徑System.out.println("讀取絕對路徑"+file.getAbsolutePath());//讀取文件的父級路徑()System.out.println("讀取文件的父級路徑"+new?File(file.getAbsolutePath()).getParent());//讀取文件的大小System.out.println("讀取文件的大小"+file.length()+"byte");//獲取的字節System.out.println("讀取文件的大小"+(float)file.length()/1024+"KB");//判斷文件是否被隱藏System.out.println("判斷文件是否被隱藏"+file.isHidden());//判斷文件是否可讀System.out.println("判斷文件是否可讀"+file.canRead());//判斷文件是否可寫System.out.println("判斷文件是否可寫"+file.canWrite());//判斷文件是都為文件夾System.out.println("判斷文件是都為文件夾:"+file.isDirectory());} }4.文件屬性的 設置
public?class?SetFileProperty?{public?static?void?main(String[]?args)?{File?file?=?new?File("test.file");if?(file.exists())?{?//?判斷文件是否存在//?設置文件的設定為可寫file.setWritable(true);//?設置文件的設定為可讀file.setReadable(true);//?設置文件的設定只讀file.setReadOnly();}?else?{}} }5.遍歷文件夾
public?class?Scaner?{ /***文件的遍歷?*/public?static?void?main(String[]?args)?{printFiles(new?File("/D:/apache-tomcat-8.0.28"),1);//絕對路徑//printFiles(new?File("../FileScaner"),1);}public?static?void?printFiles(File?dir,int?tab)?{//判斷是否為文件夾?如果是文件的話,輸出名稱,如果是文件夾的話,則遞歸,輸出下層if?(dir.isDirectory())?{File?next[]?=?dir.listFiles();for?(int?i?=?0;?i?<?next.length;?i++)?{for?(int?j?=?0;?j?<tab;?j++)?{System.out.print("!-");}System.out.println(next[i].getName());if?(next[i].isDirectory())?{printFiles(next[i],tab+1);}}}} }
6.文件的簡單讀寫
亂碼問題,要看你自帶用的是UTF-8還是用的是GBK,然后在下面選擇的時候選擇自己使用的?
InputStreamReader?isr?=?new?InputStreamReader(fis,?"GBK"); public?class?ReadFile?{public?static?void?main(String[]?args)?{/***?讀取文件*/File?file?=?new?File("text.txt");if?(file.exists())?{//?判斷文件是否存在System.out.println("exist");//?創建文件輸入流//?1.首先創建文件的輸入流?2.文件輸入流的Read?3.穿件具有緩沖功能的輸入流try?{FileInputStream?fis?=?new?FileInputStream(file);InputStreamReader?isr?=?new?InputStreamReader(fis,?"GBK");BufferedReader?br?=?new?BufferedReader(isr);//?臨時存放讀取到的數據String?line;//?判斷讀取的最后一行,是否為空,然後輸出while?((line?=?br.readLine())?!=?null)?{System.out.println(line);}//?先打開的后關閉br.close();isr.close();fis.close();}?catch?(FileNotFoundException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(UnsupportedEncodingException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}}try?{File?newfile?=?new?File("newText.txt");FileOutputStream?fos?=?new?FileOutputStream(newfile);OutputStreamWriter?osw?=?new?OutputStreamWriter(fos,?"GBK");BufferedWriter?bw?=?new?BufferedWriter(osw);//每次寫入,把上一次的文件覆蓋bw.write("木蘭詩?/?木蘭辭\n");bw.write("唧唧復唧唧,木蘭當戶織。不聞機杼聲,惟聞女嘆息。\n(惟聞?通:唯)問女何所思,問女何所憶。女亦無所思,女亦無所憶。\n昨夜見軍帖,可汗大點兵,軍書十二卷,卷卷有爺名。阿爺無大兒,木蘭無長兄,愿為市鞍馬,從此替爺征。東市買駿馬,西市買鞍韉,南市買轡頭,北市買長鞭。\n旦辭爺娘去,暮宿黃河邊,不聞爺娘喚女聲,但聞黃河流水鳴濺濺。旦辭黃河去,暮至黑山頭,不聞爺娘喚女聲,但聞燕山胡騎鳴啾啾。\n");bw.write(" 萬里赴戎機,關山度若飛。朔氣傳金柝,寒光照鐵衣。將軍百戰死,壯士十年歸。歸來見天子,天子坐明堂。\n策勛十二轉,賞賜百千強。可汗問所欲,木蘭不用尚書郎,愿馳千里足,送兒還故鄉。(一作:愿借明駝千里足)\n");bw.write(" 爺娘聞女來,出郭相扶將;阿姊聞妹來,當戶理紅妝;小弟聞姊來,磨刀霍霍向豬羊。\n開我東閣門,坐我西閣床,脫我戰時袍,著我舊時裳。當窗理云鬢,對鏡貼花黃。出門看火伴,火伴皆驚忙:同行十二年,不知木蘭是女郎。(貼?通:帖;驚忙?一作:惶)雄兔腳撲朔,雌兔眼迷離;雙兔傍地走,安能辨我是雄雌?");bw.close();osw.close();fos.close();System.out.println("寫入完成");}?catch?(FileNotFoundException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(UnsupportedEncodingException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}?catch?(IOException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}} }百度云的源代碼,需要的朋友可以看一下。http://pan.baidu.com/s/1qWOS3FI
Android中的文件操作:http://my.oschina.net/TAOH/blog/533005
轉載于:https://my.oschina.net/TAOH/blog/531148
總結
以上是生活随笔為你收集整理的java基础的文件操作(File类)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html table表头升序 降序,jq
- 下一篇: 【瑞吉外卖】学习笔记-day5:(三)手