java中的文件操作總結(jié)(干貨)
File類簡介
package com.file; import java.io.File; import java.io.IOException; /** * Created by elijahliu on 2017/2/10. */ public class filetest { public static void main(String[] args) { File file = new File("hello.txt"); //是否存在 if (file.exists()) { //文件 System.out.println(file.isFile()); //路徑(文件夾) System.out.println(file.isDirectory()); File nameto = new File("new Hello.txt"); file.renameTo(nameto);//這里就是重命名文件的操作,直接新建一個(gè)file對(duì)象然后使用renameTo方法可以重命名文件 } else { System.out.println("文件不存在"); try { file.createNewFile(); System.out.println("文件已被創(chuàng)建"); } catch (IOException e) { System.out.println("文件無法創(chuàng)建"); } } if (file.exists()) { //刪除文件 file.delete(); System.out.println("刪除文件"); } else { } } }
文件夾操作
package com.file; import java.io.File; /** * Created by elijahliu on 2017/2/11. */ public class HelloFolder { public static void main(String[] args) { File folder = new File("my new folder"); if (folder.mkdir()) {//創(chuàng)建文件夾 判斷是否成功 System.out.println("文件夾創(chuàng)建完成"); File newfolder = new File("myn new foleder - new"); folder.renameTo(newfolder);//這里重命名了文件夾 文件夾的重命名是可以單獨(dú)更改一級(jí)的文件夾名的 而這一級(jí)下面的文件夾不變 保存目錄結(jié)構(gòu) if (folder.delete()) { System.out.print("done");//這里的刪除只能刪除空文件夾,如果文件夾中有東西,那么則不能刪除,不問三七二十一直接刪除一個(gè)非空文件夾是非常不負(fù)責(zé)任的 } else { System.out.println("fail"); } }else{ if (folder.exists()) { System.out.println("文件夾已經(jīng)存在不用創(chuàng)建"); }else{ System.out.println("文件夾創(chuàng)建失敗"); } } File folders = new File("my new folder/one/two/three/main"); folders.mkdirs();//在java中用mkdir只能創(chuàng)建一個(gè),mkdirs可以創(chuàng)建多級(jí)目錄 } }
文件屬性設(shè)置
package com.file; import java.io.File; /** * Created by elijahliu on 2017/2/11. */ 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();//只讀 } } }
遍歷文件夾
public void printFiles(File dir,int tab) {//tab為不同目錄結(jié)構(gòu)的縮進(jìn)量 if (dir.isDirectory()) { File next[] = dir.listFiles();//判斷如果是目錄 則返回目錄所有的文件名數(shù)組用于遍歷文件夾 for (int i = 0;i<next.length;i++) {//層次縮進(jìn)輸出 System.out.print("---"); } for(int i = 0;i<next.length;i++) {//這里用了遞歸獲取目錄結(jié)構(gòu) System.out.println(next[i].getName()); if (next[i].isFile()) { printFiles(next[i],++tab); } } } }
文件簡單讀寫
package com.file; import java.io.*; /** * Created by elijahliu on 2017/2/11. */ public class ReadFile { public static void main(String[] args) { File file = new File("new Hello.txt"); if(file.exists()){ System.err.print("exsit"); try (FileInputStream fis = new FileInputStream(file)) {//文件輸入流 這是字節(jié)流 InputStreamReader isr = new InputStreamReader(fis,"UTF-8");//inputstreamReader是一個(gè)字節(jié)流,將字節(jié)流和字符流轉(zhuǎn)化的時(shí)候,就需要制定一個(gè)編碼方式,不然就會(huì)亂碼 BufferedReader br = new BufferedReader(isr);//字符緩沖區(qū) String line; while((line = br.readLine())!=null){//這里將緩沖區(qū)里的內(nèi)容如果非空就讀出來打印 System.out.println(line); } br.close();//最后將各個(gè)線程關(guān)閉 isr.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } File newfile = new File("newtext.txt"); try { FileOutputStream fos = new FileOutputStream(newfile);//這里如果文件不存在會(huì)自動(dòng)創(chuàng)建文件 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//和讀取一樣這里是轉(zhuǎn)化的是字節(jié)和字符流 BufferedWriter bw = new BufferedWriter(osw);//這里是寫入緩沖區(qū) bw.write("厲害了我的哥");//寫入字符串 bw.close();//和上面一樣 這里后打開的先關(guān)閉 先打開的后關(guān)閉 osw.close(); fos.close(); System.out.println("done"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java如何獲取request中json數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java如何獲取request中json數(shù)據(jù)的相關(guān)資料,文中通過代碼示例以及圖文將獲取的方法介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java注冊(cè)郵箱激活驗(yàn)證實(shí)現(xiàn)代碼
這篇文章主要介紹了Java注冊(cè)郵箱激活驗(yàn)證實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12Springboot RestTemplate 簡單使用解析
這篇文章主要介紹了Springboot RestTemplate 簡單使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08利用feign調(diào)用返回object類型轉(zhuǎn)換成實(shí)體
這篇文章主要介紹了利用feign調(diào)用返回object類型轉(zhuǎn)換成實(shí)體,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03關(guān)于json解析多層嵌套并轉(zhuǎn)為對(duì)應(yīng)類(List)
在進(jìn)行JSON解析時(shí),遇到多層嵌套結(jié)構(gòu)可通過遞歸或?qū)S脦靵韺?shí)現(xiàn),重要的是將嵌套的JSON對(duì)象準(zhǔn)確轉(zhuǎn)化為對(duì)應(yīng)的Java類,通常需要依賴如Gson或Jackson等庫,將JSONObject轉(zhuǎn)為JavaBean時(shí),關(guān)注字段匹配與數(shù)據(jù)類型轉(zhuǎn)換2024-10-10java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort)
這篇文章主要介紹了java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01restTemplate未設(shè)置連接數(shù)導(dǎo)致服務(wù)雪崩問題以及解決
面對(duì)線上問題,仔細(xì)分析原因,及時(shí)調(diào)整配置,能有效解決問題,本文詳細(xì)描述了線上遇到流量突增引發(fā)的問題,通過查看代碼和連接池信息,分析出問題的原因是連接池滿了,連接池大小配置不足以應(yīng)對(duì)大并發(fā)流量,通過調(diào)整連接池大小配置2024-10-10