日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

06-Java 本地文件操作

發(fā)布時(shí)間:2024/4/13 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 06-Java 本地文件操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。