06-Java 本地文件操作
1、File類簡(jiǎn)介
? 創(chuàng)建好:File file=new File("hello.txt"); 后,按住Ctrl鍵、單擊File。會(huì)出現(xiàn)File的源代碼。
在視圖左下角雙擊“outline”大綱最大化后會(huì)出現(xiàn)文件所具有的方法,帶有綠色的點(diǎn),表明方法是公開的,即public 。
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {System.out.println(file.isFile());//文件可以是文件System.out.println(file.isDirectory());//也可以是文件夾(路徑)}else {System.out.println("文件不存在"); //結(jié)果是不存在的!?
2、文件的創(chuàng)建、刪除、重命名。
(1)創(chuàng)建和刪除:?
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {// System.out.println(file.isFile());//文件可以是文件 // System.out.println(file.isDirectory());//也可以是文件夾(路徑)file.delete();//文件刪除System.out.println("文件刪除成功");}else {System.out.println("文件不存在");try {file.createNewFile();System.out.println("文件已經(jīng)成功創(chuàng)建");} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("文件無(wú)法創(chuàng)建");?
(2)重命名:(注意:文件夾結(jié)構(gòu)必須處于同一個(gè)分區(qū),文件處于不同的分區(qū),需要使用的是文件的拷貝而不是重命名)
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("hello.txt");if (file.exists()) {File nameto=new File("new hello.txt");//重命名 file.renameTo(nameto); // System.out.println(file.isFile());//文件可以是文件 // System.out.println(file.isDirectory());//也可以是文件夾(路徑)//file.delete();//文件刪除//System.out.println("文件刪除成功");}else {System.out.println("文件不存在");try {file.createNewFile();System.out.println("文件已經(jīng)成功創(chuàng)建");} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("文件無(wú)法創(chuàng)建");}?
3、文件夾的創(chuàng)建、刪除、重命名。
1、
public static void main(String[] args) {File folder =new File("my new folder");//文件夾的創(chuàng)建if (folder.mkdirs()) {System.out.println("文件夾創(chuàng)建完成");}else {if (folder.exists()) {System.out.println("文件夾已經(jīng)存在不用創(chuàng)建");}else {System.out.println("文件夾創(chuàng)建失敗");?
2、
File folder =new File("my new folder");File newfolder=new File("my new folder-new");//重命名if (folder.renameTo(newfolder)) {System.out.println("重命名成功");}else {System.out.println("重命名失敗");?
3、
File folder =new File("my new folder");if (folder.delete()) {//刪除(只能刪空文件夾啊!)System.out.println("delete suceeded");}else {System.out.println("delete failed");?
4、文件屬性的讀取
(1)創(chuàng)建一個(gè)文件:在右擊工程、new、選untitled text file 、輸入內(nèi)容后、另存為(選中所屬工程名)、重命名~
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()); // 讀取文件(相對(duì))路徑System.out.println("讀取文件路徑"+file.getPath()); // 讀取文件絕對(duì)路徑System.out.println("讀取文件絕對(duì)路徑"+file.getAbsolutePath()); // 獲取文件父級(jí)路徑System.out.println("獲取文件父級(jí)路徑"+new File(file.getAbsolutePath()).getParent()); // 讀取文件大小System.out.println("讀取文件大小"+file.length()+"byte");System.out.println("讀取文件大小"+(float)file.length()/1000+"KB"); // 判斷文件是否被隱藏System.out.println("判斷文件是否被隱藏"+file.isHidden()); // 判斷文件是否可讀System.out.println("判斷文件是否可讀"+file.canRead()); // 判斷文件是否可寫System.out.println("判斷文件是否可寫"+file.canWrite()); // 判斷文件是否為文件夾System.out.println("判斷文件是否為文件夾"+file.isDirectory());?
結(jié)果:
判斷文件是否存在true
讀取文件名稱text.txt
讀取文件路徑text.txt
讀取文件絕對(duì)路徑C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty\text.txt
獲取文件父級(jí)路徑C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty
讀取文件大小14byte
讀取文件大小0.014KB
判斷文件是否被隱藏false
判斷文件是否可讀true
判斷文件是否可寫true(如果將文件設(shè)置為只讀的話 ,那么就是false !)
判斷文件是否為文件夾false
?
?
1、? 文件屬性的設(shè)置
File file=new File("test.file"); //也是要手動(dòng)新建一個(gè)文件的!if (file.exists()) {//將文件設(shè)定為可寫(前提需要先將它設(shè)置為不可寫!)file.setWritable(true);//or false//將文件設(shè)定為可讀file.setReadable(true);//將文件設(shè)定為只讀(運(yùn)行一下語(yǔ)句時(shí)需要將上面兩個(gè)語(yǔ)句注釋掉~)file.setReadOnly();}2、? 遍歷文件夾:
public static void main(String[] args) {// TODO Auto-generated method stubprintFiles(new File("../FileScanner"),1);}public static void printFiles(File dir,int tab) {//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("|---");//去掉ln。 }System.out.println(next[i].getName());if (next[i].isDirectory()) {printFiles(next[i],tab+1);?
結(jié)果:
|---.classpath
|---.project
|---.settings
|---|---org.eclipse.jdt.core.prefs
|---bin
|---|---com
|---|---|---jikexueyuan
|---|---|---|---filescan
|---|---|---|---|---main
|---|---|---|---|---|---Scanner.class
|---src
|---|---com
|---|---|---jikexueyuan
|---|---|---|---filescan
|---|---|---|---|---main
|---|---|---|---|---|---Scanner.java
?
3、? 文件的簡(jiǎn)單讀寫:
(1)讀::
???
public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("text.txt");if (file.exists()) {System.err.println("exist");try {FileInputStream fis =new FileInputStream(file);InputStreamReader isr=new InputStreamReader(fis, "UTF-8");BufferedReader br=new BufferedReader(isr);String line;while ((line=br.readLine())!=null) {System.out.println(line);}br.close();isr.close();fis.close();//先打開的后關(guān)閉,后打開的先關(guān)閉} catch (FileNotFoundException e){e.printStackTrace();}catch ( UnsupportedEncodingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();(2)寫:
??
File newfile =new File("newtext.txt");FileOutputStream fos =new FileOutputStream(newfile);OutputStreamWriter osw =new OutputStreamWriter(fos,"UTF-8");BufferedWriter bw=new BufferedWriter(osw);try {bw.write("長(zhǎng)歌行 漢樂府\n");bw.write("青青園中葵,朝露待日晞。\n");bw.write("陽(yáng)春布德澤,萬(wàn)物生光輝。\n");bw.write("常恐秋節(jié)至,焜黃華葉衰。\n");bw.write("百川東到海,何時(shí)復(fù)西歸?\n");bw.write("少壯不努力,老大徒傷悲。\n");bw.close();osw.close();fos.close();System.out.println("寫入完成");} catch (FileNotFoundException e){e.printStackTrace();}catch ( UnsupportedEncodingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();?
總結(jié)
以上是生活随笔為你收集整理的06-Java 本地文件操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图的色数
- 下一篇: RxJava系列四(过滤操作符)