静态变量
pathSeparator,pathSeparatorChar //系统相关路径分隔符,windows为;
separator,separatorChar //系统相关名称分隔符,windows为\
构造函数
File(File parent, String child) //抽象文件,路径字符串
File(String pathname) //完整路径字符串
File(URI uri) //URL对象
File(String parent, String child) //路径字符串,路径字符串
创建
boolean createNewFile() //创建一个空文件
static File createTempFile(String prefix, String suffix) //在默认目录下依据给出的前缀后缀创建空文件
static File createTempFile(String prefix, String suffix,File directory) //在给定文件夹下依据给出的前缀后缀创建空文件
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:\ */