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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java IO流学习总结二:File

發布時間:2024/9/30 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java IO流学习总结二:File 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/54581478
本文出自【趙彥軍的博客】

Java IO流學習總結一:輸入輸出流
Java IO流學習總結二:File
Java IO流學習總結三:緩沖流-BufferedInputStream、BufferedOutputStream
Java IO流學習總結四:緩沖流-BufferedReader、BufferedWriter
Java IO流學習總結五:轉換流-InputStreamReader、OutputStreamWriter
Java IO流學習總結六:ByteArrayInputStream、ByteArrayOutputStream
Java IO流學習總結七:Commons IO 2.5-FileUtils

2021年 Java Okio-更加高效易用的IO庫

Java File類的功能非常強大,利用java基本上可以對文件進行所有操作。
首先來看File類的構造函數的源碼

/*** Internal constructor for already-normalized pathname strings.*/private File(String pathname, int prefixLength) {this.path = pathname;this.prefixLength = prefixLength;}/*** Internal constructor for already-normalized pathname strings.* The parameter order is used to disambiguate this method from the* public(File, String) constructor.*/private File(String child, File parent) {assert parent.path != null;assert (!parent.path.equals(""));this.path = fs.resolve(parent.path, child);this.prefixLength = parent.prefixLength;}/*** Creates a new <code>File</code> instance by converting the given* pathname string into an abstract pathname. If the given string is* the empty string, then the result is the empty abstract pathname.** @param pathname A pathname string* @throws NullPointerException* If the <code>pathname</code> argument is <code>null</code>*/public File(String pathname) {if (pathname == null) {throw new NullPointerException();}this.path = fs.normalize(pathname);this.prefixLength = fs.prefixLength(this.path);}/*** @param parent The parent pathname string* @param child The child pathname string* @throws NullPointerException* If <code>child</code> is <code>null</code>*/public File(String parent, String child) {if (child == null) {throw new NullPointerException();}if (parent != null && !parent.isEmpty()) {this.path = fs.resolve(fs.normalize(parent),fs.normalize(child));} else {this.path = fs.normalize(child);}this.prefixLength = fs.prefixLength(this.path);}/*** @param parent The parent abstract pathname* @param child The child pathname string* @throws NullPointerException* If <code>child</code> is <code>null</code>*/public File(File parent, String child) {if (child == null) {throw new NullPointerException();}if (parent != null) {if (parent.path.equals("")) {this.path = fs.resolve(fs.getDefaultParent(),fs.normalize(child));} else {this.path = fs.resolve(parent.path,fs.normalize(child));}} else {this.path = fs.normalize(child);}this.prefixLength = fs.prefixLength(this.path);}/*** @param uri* An absolute, hierarchical URI with a scheme equal to* <tt>"file"</tt>, a non-empty path component, and undefined* authority, query, and fragment components** @throws NullPointerException* If <tt>uri</tt> is <tt>null</tt>** @throws IllegalArgumentException* If the preconditions on the parameter do not hold*/public File(URI uri) {// Check our many preconditionsif (!uri.isAbsolute())throw new IllegalArgumentException("URI is not absolute");if (uri.isOpaque())throw new IllegalArgumentException("URI is not hierarchical");String scheme = uri.getScheme();if ((scheme == null) || !scheme.equalsIgnoreCase("file"))throw new IllegalArgumentException("URI scheme is not \"file\"");if (uri.getAuthority() != null)throw new IllegalArgumentException("URI has an authority component");if (uri.getFragment() != null)throw new IllegalArgumentException("URI has a fragment component");if (uri.getQuery() != null)throw new IllegalArgumentException("URI has a query component");String p = uri.getPath();if (p.equals(""))throw new IllegalArgumentException("URI path component is empty");// Okay, now initializep = fs.fromURIPath(p);if (File.separatorChar != '/')p = p.replace('/', File.separatorChar);this.path = fs.normalize(p);this.prefixLength = fs.prefixLength(this.path);}

從源碼可以看出File類的構造函數有6個,精簡如下

public File(String pathname) //文件的絕對路徑 public File(URI uri) //文件的URI地址public File(String parent, String child) //指定父文件絕對路徑、子文件絕對路徑 public File(File parent, String child) //指定父文件、子文件相對路徑//下面這兩個是File類中私有的構造函數,外面不能調用 private File(String child, File parent) private File(String pathname, int prefixLength)

現在就看的比較清楚了,6個構造函數,可以分為2類。4個公共構造函數,2個私有構造函數。

構造函數1:

//電腦d盤中的cat.png 圖片的路徑 String filePath1 = "D:/cat.png" ; File file = new File( filePath1 ) ;

構造函數2:

String parentFilePath = "E:/cat" ;String childFilePath = "small_cat.txt" ;//創建parentFile文件 File parentFile = new File( parentFilePath ) ; parentFile.mkdir() ;//如果parentFile不存在,就會報異常 File file = new File( parentFilePath , childFilePath ) ;try {file.createNewFile() ; } catch (IOException e) {e.printStackTrace(); }

構造函數3:

String parentFilePath = "E:/cat" ;//構造父文件 File parent = new File( parentFilePath ) ; parent.mkdir(); //如果parent文件不存在,就會報異常 File file = new File( parent , "small_cat.txt" ) ;try {file.createNewFile() ; } catch (IOException e) {e.printStackTrace(); }
  • 創建目錄
boolean file.mkdir()

如果創建成功,返回 true , 創建失敗,返回false。如果這個文件夾已經存在,則返回false.
只能創建一級目錄,如果父目錄不存在,返回false.

  • 創建多級目錄
boolean file.mkdirs()

創建多級目錄,創建成功,返回true,創建失敗,返回false。如果父目錄不存在,就創建,并且返回true.

  • 創建一個新的文件
boolean file.createNewFile() ;

如果文件不存在就創建該文件,創建成功,返回 true ;創建失敗,返回false。如果這個文件已經存在,則返回false.

  • 判斷方法
boolean file.exists() //文件是否存在boolean file.isFile() //是否是文件boolean file.isDirectory() //是否是目錄boolean file.isHidden() //是否隱藏(windows上可以設置某個文件是否隱藏)boolean file.isAbsolute() //是否為絕對路徑boolean file.canRead() //是否可讀boolean file.canWrite() //是否可寫boolean file.canExecute() //是否可執行

獲取文件的信息

String file.getName() //獲取文件的名字,只是名字,沒有路徑String file.getParent() //獲取父目錄的絕對路徑,返回值是一個字符串。如果文件有父目錄,那么返回父目錄的絕對路徑,(比如:`E:\cat`) , 如果文件本身就在磁盤的根目錄,那么返回磁盤的路徑,(比如:`E:\`)。File file.getParentFile() //獲取父文件,返回值是一個File對象。long time = file.lastModified() ; //返回文件最后一次修改的時間 Date dt = new Date(time);boolean renameTo(File file) //文件命名long file.length() //返回文件的大小,單位字節boolean file.delete() //刪除文件String[] file.list() //獲取該目錄下的所有的文件的名字。如果`file`為文件,返回值為`null`,在使用時記得判空;但是如果`file`為目錄,那么返回這個目錄下所有文件的名字,只是名字,不含路徑;如果`file`是一個空目錄,返回一個長度為0的數組;從上面的結果可以看出,`list()` 方法,只是對`file`為目錄時有效,當`file`為一個文件的時候,該方法毫無意義。File[] file.listFiles() //獲取該目錄下的所有的文件。如果`file`為文件,返回值為`null`,在使用時記得判空;但是如果`file`為目錄,那么返回這個目錄下所有的文件 ;如果`file`是一個空目錄,返回一個長度為0的數組;從上面的結果可以看出,`listFiles()` 方法,只是對`file`為目錄時有效,當`file`為一個文件的時候,該方法毫無意義。

實戰經驗1: file.list() , file.listFiles()

String filePath = "E:/cat" ; File file = new File( filePath ) ; file.mkdir() ;String[] names = file.list() ;for( int i = 0 ; i < names.length ; i++ ){System.out.println( "names: " +names[i]); }File[] files = file.listFiles() ; for( int i = 0 ; i < files.length ; i++ ){System.out.println( "files: "+ files[i].getAbsolutePath() ); }

實戰經驗2:掃描F盤所有的文件

public class A3 {public static void main(String[] args) throws IOException {String filePath = "F:/" ;File file = new File( filePath ) ;getFile(file);}private static void getFile( File file ){File[] files = file.listFiles() ;for( File f : files ){if ( f.isHidden() ) continue ;if(f.isDirectory() ){getFile( f ); }else{System.out.println( f.getAbsolutePath() + " " + f.getName() );}}} }

效果圖:

在上面的實戰演練中用到了,file.list() , file.listFiles() 。這是兩個無參的方法,實際上還有兩個有參的方法,分別是

file.list(FilenameFilter filter) ;file.listFiles( FilenameFilter filter) ;file.listFiles(FileFilter filter)

FileFilter

FileFilter是io包里面的一個接口,從名字上可以看出,這個類是文件過濾功能的。
需要重寫accept方法
比如:

static class MyFileFilter implements FileFilter {MyFileFilter(){ }//pathname:文件的絕對路徑+ 文件名 , 比如:F:\安來寧 - 難得.mp3 , 或者: F:\文件夾1 @Override public boolean accept(File pathname) {return false; } }

實戰:獲取指定目錄的所有文件夾

package com.app; import java.io.File; import java.io.FileFilter; import java.io.IOException;public class A3 {public static void main(String[] args) throws IOException {String filePath = "F:/" ;File file = new File( filePath ) ;getFile(file);}/*** 獲取指定目錄的所有文件夾* @param file*/private static void getFile( File file ){MyFileFilter myFileFilter = new MyFileFilter() ;File[] files = file.listFiles( myFileFilter ) ;for( File f : files ){if ( f.isHidden() ) continue ;System.out.println( f.getAbsolutePath() );}}static class MyFileFilter implements FileFilter {MyFileFilter(){}//pathname:文件的絕對路徑+ 文件名 , 比如:F:\安來寧 - 難得.mp3 , 或者: F:\文件夾1@Overridepublic boolean accept(File pathname) {if( pathname.isDirectory() ){return true ;}return false;}}}

FilenameFilter

FileFilter是io包里面的一個接口,從名字上可以看出,這個類是文件名字過濾功能的。
需要重寫里面的accept方法。
比如:

package com.app;import java.io.File; import java.io.FilenameFilter;public class MyFilenameFilter implements FilenameFilter {//type為需要過濾的條件,比如如果type=".jpg",則只能返回后綴為jpg的文件private String type; MyFilenameFilter( String type){this.type = type ;}@Overridepublic boolean accept(File dir, String name) {//dir表示文件的當前目錄,name表示文件名;return name.endsWith( type ) ;}}

實戰:掃描出指定路徑的所有圖片

package com.app; import java.io.File; import java.io.FilenameFilter; import java.io.IOException;public class A3 {public static void main(String[] args) throws IOException {String filePath = "F:/" ;File file = new File( filePath ) ;getFile(file);}/*** 掃描出指定路徑的所有圖片* @param file*/private static void getFile( File file ){MyFilenameFilter myFileFilter = new MyFilenameFilter( ".png") ;File[] files = file.listFiles( myFileFilter ) ;for( File f : files ){if ( f.isHidden() ) continue ;System.out.println( f.getAbsolutePath() );}}static class MyFilenameFilter implements FilenameFilter {//type為需要過濾的條件,比如如果type=".jpg",則只能返回后綴為jpg的文件private String type; MyFilenameFilter( String type){this.type = type ;}@Overridepublic boolean accept(File dir, String name) {//dir表示文件的當前目錄,name表示文件名;return name.endsWith( type ) ;}}}

運行結果:

總結

以上是生活随笔為你收集整理的Java IO流学习总结二:File的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲天堂免费av | www.色天使 | 男男车车的车车网站w98免费 | 天天综合网久久 | 久久老熟女一区二区三区 | 天天操狠狠干 | 青青在线观看视频 | 夜夜骑天天操 | 日韩美女毛片 | 国产欧美日韩另类 | 国产亚洲精品成人 | 18久久 | 国产精品88av| 欧美一级免费黄色片 | 久久久久亚洲av片无码v | 丝袜视频在线观看 | 草在线视频| 色综合久久av | 51吃瓜网今日| 女人扒开屁股让男人捅 | 色婷婷国产 | 精品视频一区二区三区在线观看 | 国内精品卡一卡二卡三 | 国产男女无套免费网站 | 美女裸体网站久久久 | 欧美视频 | 欧美日韩精品在线观看 | 一二三区在线播放 | 日韩免费福利视频 | 日韩精品一区二区三区中文字幕 | 国内精品视频在线播放 | 美女扒开让男人桶爽 | 91精品国产视频 | 久久亚洲视频 | av中文网| 黄色片a| 五十路在线 | 欧美性生活在线视频 | 国产ts三人妖大战直男 | 人妻少妇一区二区 | 国产精品欧美综合 | 亚洲乱码中文字幕久久孕妇黑人 | 亚洲女优在线播放 | 国产精品无码av在线播放 | 国产一级二级三级精品 | 91人妻一区二区三区蜜臀 | 成人久久久 | 日日爱669 | 欧美大浪妇猛交饥渴大叫 | 蜜臀av色欲a片无码精品一区 | 九色网站在线观看 | 国产一级片网站 | 成人精品网 | 华人av在线 | 中国人与拘一级毛片 | 亚洲自拍在线观看 | 国产黄色影院 | 秋霞国产| 1024你懂的日韩 | 男人透女人免费视频 | 亚洲国产剧情在线观看 | 91蜜桃| 91国产精品一区 | cao在线视频 | 国产第四页 | 欧美精品自拍视频 | 欧美日韩激情在线 | 久久理论视频 | 北条麻妃av在线 | 欧美不卡视频在线观看 | 校园春色综合 | 国产麻豆乱码精品一区二区三区 | 天天艹| 九九爱视频 | 国产精选久久久 | 污视频在线免费观看 | 少妇搡bbbb搡bbb搡小说 | 内谢少妇xxxxx8老少交视频 | 日韩一级片免费在线观看 | 亚洲视频不卡 | 碧蓝之海动漫在线观看免费高清 | 午夜a区| 99精品国产一区二区 | 影音先锋中文字幕在线 | 香蕉视频官网 | 一区二区三区四区高清视频 | 日本在线成人 | 动漫av一区 | 69视频入口| 饥渴少妇伦色诱公 | 久久国产情侣 | 精品成人无码一区二区三区 | 91av视频免费观看 | 91丨九色丨蝌蚪丨对白 | 国产九九精品 | 免费观看视频一区二区 | 人妻 日韩 欧美 综合 制服 | 欧美亚洲精品一区 | 成人视屏在线 |