Java IO流學習總結二:File。本站提示廣大學習愛好者:(Java IO流學習總結二:File)文章只能為提供參考,不一定能成為您想要的結果。以下是Java IO流學習總結二:File正文
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/54581478
本文出自【趙彥軍的博客】
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 preconditions
if (!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 initialize
p = 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`為一個文件的時分,該辦法毫有意義。實戰經歷: 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() );
}
效果圖:
