File類是java.io包下表明與平臺無關的文件和目錄,也就是說,若是但願在程序中操做文件和目錄,均可以經過File類來完成。值得指出的是,無論是文件仍是目錄都是使用File來操做的,File能新建、刪除、重命名文件和目錄,File不能訪問文件內容自己。若是須要訪問文件內容自己,則須要使用輸入/輸出流。html
File類相關的方法參考連接:https://docs.oracle.com/javase/9/docs/api/overview-summary.htmljava
Modifier and Type | Field | Description |
---|---|---|
static String |
pathSeparator |
The system-dependent path-separator character, represented as a string for convenience.
|
static char |
pathSeparatorChar |
The system-dependent path-separator character.
|
static String |
separator |
The system-dependent default name-separator character, represented as a string for convenience.
|
static char |
separatorChar |
The system-dependent default name-separator character.
|
Constructor | Description |
---|---|
File(File parent, String child) |
Creates a new
File instance from a parent abstract pathname and a child pathname string.
|
File(String pathname) |
Creates a new
File instance by converting the given pathname string into an abstract pathname.
|
File(String parent, String child) |
Creates a new
File instance from a parent pathname string and a child pathname string.
|
File(URI uri) |
Creates a new
File instance by converting the given
file: URI into an abstract pathname.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
canExecute() |
Tests whether the application can execute the file denoted by this abstract pathname.
|
boolean |
canRead() |
Tests whether the application can read the file denoted by this abstract pathname.
|
boolean |
canWrite() |
Tests whether the application can modify the file denoted by this abstract pathname.
|
int |
compareTo(File pathname) |
Compares two abstract pathnames lexicographically.
|
boolean |
createNewFile() |
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
|
static File |
createTempFile(String prefix,String suffix) |
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
|
static File |
createTempFile(String prefix,String suffix,File directory) |
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
|
boolean |
delete() |
Deletes the file or directory denoted by this abstract pathname.
|
void |
deleteOnExit() |
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
|
boolean |
equals(Object obj) |
Tests this abstract pathname for equality with the given object.
|
boolean |
exists() |
Tests whether the file or directory denoted by this abstract pathname exists.
|
File |
getAbsoluteFile() |
Returns the absolute form of this abstract pathname.
|
String |
getAbsolutePath() |
Returns the absolute pathname string of this abstract pathname.
|
File |
getCanonicalFile() |
Returns the canonical form of this abstract pathname.
|
String |
getCanonicalPath() |
Returns the canonical pathname string of this abstract pathname.
|
long |
getFreeSpace() |
Returns the number of unallocated bytes in the partition
named by this abstract path name.
|
String |
getName() |
Returns the name of the file or directory denoted by this abstract pathname.
|
String |
getParent() |
Returns the pathname string of this abstract pathname's parent, or
null if this pathname does not name a parent directory.
|
File |
getParentFile() |
Returns the abstract pathname of this abstract pathname's parent, or
null if this pathname does not name a parent directory.
|
String |
getPath() |
Converts this abstract pathname into a pathname string.
|
long |
getTotalSpace() |
Returns the size of the partition
named by this abstract pathname.
|
long |
getUsableSpace() |
Returns the number of bytes available to this virtual machine on the partition
named by this abstract pathname.
|
int |
hashCode() |
Computes a hash code for this abstract pathname.
|
boolean |
isAbsolute() |
Tests whether this abstract pathname is absolute.
|
boolean |
isDirectory() |
Tests whether the file denoted by this abstract pathname is a directory.
|
boolean |
isFile() |
Tests whether the file denoted by this abstract pathname is a normal file.
|
boolean |
isHidden() |
Tests whether the file named by this abstract pathname is a hidden file.
|
long |
lastModified() |
Returns the time that the file denoted by this abstract pathname was last modified.
|
long |
length() |
Returns the length of the file denoted by this abstract pathname.
|
String[] |
list() |
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
|
String[] |
list(FilenameFilter filter) |
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
|
File[] |
listFiles() |
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
|
File[] |
listFiles(FileFilter filter) |
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
|
File[] |
listFiles(FilenameFilter filter) |
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
|
static File[] |
listRoots() |
List the available filesystem roots.
|
boolean |
mkdir() |
Creates the directory named by this abstract pathname.
|
boolean |
mkdirs() |
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
|
boolean |
renameTo(File dest) |
Renames the file denoted by this abstract pathname.
|
boolean |
setExecutable(boolean executable) |
A convenience method to set the owner's execute permission for this abstract pathname.
|
boolean |
setExecutable(boolean executable, boolean ownerOnly) |
Sets the owner's or everybody's execute permission for this abstract pathname.
|
boolean |
setLastModified(long time) |
Sets the last-modified time of the file or directory named by this abstract pathname.
|
boolean |
setReadable(boolean readable) |
A convenience method to set the owner's read permission for this abstract pathname.
|
boolean |
setReadable(boolean readable, boolean ownerOnly) |
Sets the owner's or everybody's read permission for this abstract pathname.
|
boolean |
setReadOnly() |
Marks the file or directory named by this abstract pathname so that only read operations are allowed.
|
boolean |
setWritable(boolean writable) |
A convenience method to set the owner's write permission for this abstract pathname.
|
boolean |
setWritable(boolean writable, boolean ownerOnly) |
Sets the owner's or everybody's write permission for this abstract pathname.
|
Path |
toPath() |
Returns a
java.nio.file.Path object constructed from the this abstract path.
|
String |
toString() |
Returns the pathname string of this abstract pathname.
|
URI |
toURI() |
Constructs a
file: URI that represents this abstract pathname.
|
URL |
toURL() |
一、訪問文件和目錄 api
1 package com.zyjhandsome.io; 2 3 import java.io.*; 4 5 public class FileTest { 6 7 public static void main(String[] args) throws IOException 8 { 9 // 以當前路徑來建立一個File對象 10 File file = new File("."); 11 // 直接獲取文件名, 輸出一點 12 System.out.println(file.getName()); 13 // 獲取相對路徑的父路徑可能出錯, 下面代碼輸出null 14 System.out.println(file.getParent()); 15 // 獲取絕對路徑 16 System.out.println(file.getAbsoluteFile()); 17 // 獲取絕對路徑 18 System.out.println(file.getAbsoluteFile().getParent()); 19 // 當前路徑下建立一個臨時文件 20 File tmpFile = File.createTempFile("aaa", ".txt", file); 21 // 指定當JVM退出時候刪除該文件 22 tmpFile.deleteOnExit(); 23 // 以系統當前時間做爲新文件名來建立新文件 24 File newFile = new File(System.currentTimeMillis() + ""); 25 System.out.println("newFile對象是否存在1: " + newFile.exists()); 26 // 以指定newFile對象來建立一個文件 27 newFile.createNewFile(); 28 System.out.println("newFile對象是否存在2: " + newFile.exists()); 29 // 以newFile對象來建立一個目錄,由於newFile已經存在,因此下面方法返回false, 即沒法建立該目錄 30 System.out.println("newFile.mkdir():" + newFile.mkdir()); 31 System.out.println("----------------------"); 32 // 使用list()方法列出當前路徑下的全部文件和路徑 33 String[] fileList = file.list(); 34 for (String fileName : fileList) 35 { 36 System.out.println(fileName); 37 } 38 System.out.println("----------------------"); 39 // listRoots()靜態方法列出全部的磁盤根路徑 40 File[] roots = File.listRoots(); 41 System.out.println("====系統全部根路徑以下===="); 42 for (File root : roots) 43 { 44 System.out.println(root); 45 } 46 } 47 }
1 . 2 null 3 D:\zhaoyingjun\eclipse-workspace\CollectionTest\. 4 D:\zhaoyingjun\eclipse-workspace\CollectionTest 5 newFile對象是否存在1: false 6 newFile對象是否存在2: true 7 newFile.mkdir():false 8 ---------------------- 9 .classpath 10 .project 11 .settings 12 1537712414564 13 1537712424492 14 1537712893895 15 1537712926829 16 1537712940906 17 1537713158525 18 1537713167968 19 1537774060515 20 aaa1610208071466755969.txt 21 bin 22 src 23 ---------------------- 24 ====系統全部根路徑以下==== 25 C:\ 26 D:\
二、文件過濾器oracle
在File類的list()方法中能夠接收一個FilenameFilter參數,經過該參數能夠只列出符合條件的文件。這裏的FilenameFilter接口和javax.swing.filechooser包下的FileFilter抽象類的功能很是類似,能夠把FileFilter當成是FilenameFilter的實現類。app
FilenameFilter接口裏包含了一個accept(File dir, String name)方法,該方法將依次對指定File的全部子目錄或者文件進行迭代,若是該方法返回true,則list()方法會列出該子目錄或者文件。eclipse
1 package com.zyjhandsome.io; 2 3 import java.io.*; 4 5 public class FilenameFilterTest { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 File file = new File("."); 10 // 使用Lambda表達式(目標類型是FilenameFilter)實現文件過濾器 11 // 若是文件名以.java結尾, 或者文件對應一個路徑,則返回true 12 String[] nameList = file.list((dir, name) -> 13 name.endsWith(".java") || new File(name).isDirectory()); 14 for (String name : nameList) 15 { 16 System.out.println(name); 17 } 18 } 19 }
1 .settings 2 bin 3 src