pathSeparator,pathSeparatorChar //系統相關路徑分隔符,windows爲;html
separator,separatorChar //系統相關名稱分隔符,windows爲\java
File(File parent, String child) //抽象文件,路徑字符串windows
File(String pathname) //完整路徑字符串api
File(URI uri) //URL對象oracle
File(String parent, String child) //路徑字符串,路徑字符串函數
boolean createNewFile() //建立一個空文件spa
static File createTempFile(String prefix, String suffix) //在默認目錄下依據給出的前綴後綴建立空文件code
static File createTempFile(String prefix, String suffix,File directory) //在給定文件夾下依據給出的前綴後綴建立空文件htm
boolean mkdir() //建立文件夾對象
boolean mkdirs() //建立文件夾,包括不存在的目錄路徑
package com.company.ChapterFive; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { File fa = new File("D:\\fa"); System.out.println(fa.createNewFile()); File fb = new File("fb\\fb"); System.out.println(fb.mkdir()); File fc = new File("fc\\fc"); System.out.println(fc.mkdirs()); File fd = File.createTempFile("fdd",".txt"); System.out.println(fd.getPath()); File fe = File.createTempFile("fee",".txt",fc); System.out.println(fe.getPath()); } }
結果
true false //無fc子文件夾故返回false true //自動建立不存在的目錄 C:\Users\sakura\AppData\Local\Temp\fdd833235313839199787.txt //臨時文件夾下 fc\fc\fee12493352852379829354.txt
boolean delete()
void deleteOnExit() //虛擬機終止時刪除
boolean renameTo(File dest) //重命名所表示的文件
boolean setExecutable(boolean executable)
boolean setExecutable(boolean executable, boolean ownerOnly) //設置可執行權限
boolean setLastModified(long time) //設置最後修改時間
boolean setReadable(boolean readable)
boolean setReadable(boolean readable, boolean ownerOnly) //設置可讀權限
boolean setReadOnly() //設爲只讀文件
boolean setWritable(boolean writable)
boolean setWritable(boolean writable, boolean ownerOnly) //設置可寫權限
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException{ File f = new File("f.txt"); f.createNewFile(); File fa = new File("fa.txt"); if(f.renameTo(fa)) System.out.println("改名成功!"); System.out.println(fa.getName()); if(fa.setExecutable(true)) System.out.println("canExecute:設置成功"); if(fa.setReadable(true)) System.out.println("canRead:設置成功"); if(fa.setWritable(false,false)) System.out.println("canWrite:設置成功"); if(fa.setReadOnly()) System.out.println("readOnly:設置成功"); if(fa.setLastModified(100)) System.out.println("lastModified:"+fa.lastModified()); } } //結果 /* 改名成功! fa.txt canExecute:設置成功 canRead:設置成功 canWrite:設置成功 readOnly:設置成功 lastModified:100 */
File getAbsoluteFile()
String getAbsolutePath() //完整路徑
File getCanonicalFile()
String getCanonicalPath() //規範路徑
String getName() //文件名
String getParent()
File getParentFile() //父文件
String getPath() //相對文件路徑
long getTotalSpace() //文件所在磁盤所有空間
long getFreeSpace() //文件所在磁盤剩餘空間
long getUsableSpace() //文件所在磁盤剩餘空間
import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException{ File f = new File("f\\f"); File fa = new File ("f\\f\\fa"); f.mkdirs(); fa.createNewFile(); System.out.println(fa.getAbsolutePath()); System.out.println(fa.getCanonicalPath()); System.out.println(fa.getName()); System.out.println(fa.getParent()); System.out.println(fa.getPath()); System.out.println(""+fa.getFreeSpace()/(1024*1024*1024)+"GB"); System.out.println(""+fa.getUsableSpace()/(1024*1024*1024)+"GB"); System.out.println(""+fa.getTotalSpace()/(1024*1024*1024)+"GB"); } } //結果 /* D:\Code\ThinkinJava\ChapterFive\f\f\fa D:\Code\ThinkinJava\ChapterFive\f\f\fa fa f\f f\f\fa 872GB 872GB 930GB */
long lastModified() //1970到最後修改時間的毫秒數
long length() //文件字節數
public class Test { public static void main(String[] args) throws IOException{ File fa = new File("fa.txt"); //寫有helloworld System.out.println(fa.length()); //結果爲10 System.out.println(fa.lastModified()); //結果爲1564369064711,從1970年到如今的毫秒數 } }
boolean canExecute() //是否可執行
boolean canRead() //是否可讀
boolean canWrite() //是否可寫
boolean exists() //是否存在
boolean isAbsolute() //是否爲完整路徑
boolean isDirectory() //是否爲文件夾
boolean isFile() //是否爲文件
boolean isHidden() //是否隱藏
public class Test { public static void main(String[] args) throws IOException{ File fa = new File("fa.txt"); //寫有helloworld System.out.println("exists:"+fa.exists()); System.out.println("isAbsolute:"+fa.isAbsolute()); System.out.println("isDirectory:"+fa.isDirectory()); System.out.println("isFile:"+fa.isFile()); System.out.println("isHidden:"+fa.isHidden()); } } //結果 /* exists:true isAbsolute:false isDirectory:false isFile:true isHidden:false */
String[] list()
String[] list(FilenameFilter filter)
File[] listFiles()
File[] listFiles(FileFilter filter)
File[] listFiles(FilenameFilter filter) //返回子文件列表,參數是過濾器,實現一個accept方法,返回true的文件將被放入列表中
staticFile[] listRoots() //返回文件系統根目錄列表
import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException{ File fa = new File("C:\\Program Files\\Intel\\Media SDK"); String list[] = fa.list(new myFilter()); for (String s:list) { System.out.println(s); } File flist[] = fa.listFiles(new FileFilter(){ public boolean accept(File FilePath){ return FilePath.getName().endsWith("vp"); } }); for (File fileInflist:flist) { System.out.println(fileInflist.getPath()); } for (File rootFile:File.listRoots()) { System.out.println(rootFile.getAbsolutePath()); } } } class myFilter implements FilenameFilter{ public boolean accept(File dir,String name){ return name.endsWith("dll"); } } //結果 /* libmfxhw32.dll libmfxhw64.dll mfxplugin32_hw.dll mfxplugin64_hw.dll mfx_mft_h264ve_32.dll mfx_mft_h264ve_64.dll mfx_mft_h265ve_32.dll mfx_mft_h265ve_64.dll mfx_mft_mjpgvd_32.dll mfx_mft_mjpgvd_64.dll mfx_mft_vp8vd_32.dll mfx_mft_vp8vd_64.dll mfx_mft_vp9vd_32.dll mfx_mft_vp9vd_64.dll C:\Program Files\Intel\Media SDK\cpa_32.vp C:\Program Files\Intel\Media SDK\cpa_64.vp C:\Program Files\Intel\Media SDK\dev_32.vp C:\Program Files\Intel\Media SDK\dev_64.vp C:\Program Files\Intel\Media SDK\h265e_32.vp C:\Program Files\Intel\Media SDK\h265e_64.vp C:\Program Files\Intel\Media SDK\he_32.vp C:\Program Files\Intel\Media SDK\he_64.vp C:\Program Files\Intel\Media SDK\mj_32.vp C:\Program Files\Intel\Media SDK\mj_64.vp C:\ D:\ */