import java.io.File;
创建方法1.boolean createNewFile() 不存在返回true 存在返回false
2.boolean mkdir() 创建目录
3.boolean mkdirs() 创建多级目录
删除方法
1.boolean delete()
2.boolean deleteOnExit() 文件使用完成后删除
判断方法
1.boolean canExecute()判断文件是否可执行
2.boolean canRead()判断文件是否可读
3.boolean canWrite() 判断文件是否可写
4.boolean exists() 判断文件是否存在
5.boolean isDirectory()
6.boolean isFile()
7.boolean isHidden()
8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断
获取方法
1.String getName() //返回的路径名的名称序列的最后一个名字,这意味着表示此抽象路径名的文件或目录的名称被返回
2.String getPath() //返回此抽象路径名的字符串形式3.String getAbsolutePath() //返回此抽象路径名的绝对路径名字符串
4.String getParent()//如果没有父目录返回null
5.long lastModified()//获取最后一次修改的时间
6.long length()
7.boolean renameTo(File f)
8.File[] liseRoots()//获取机器盘符
9.String[] list()
10.String[] list(FilenameFilter filter)
实例:
1、getPath()与getAbsolutePath()的区别
public static void test1() {
File file1 = new File( " .\\test1.txt " );
File file2 = new File( " D:\\workspace\\test\\test1.txt " );
System.out.println( " -----默认相对路径:取得路径不同------ " );
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println( " -----默认绝对路径:取得路径相同------ " );
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
}
2、创建File文件(根据目录地址依次创建目录和文件)
/*** 创建文件* @param file* @return* @throws IOException*/
public static boolean createFile(File file) throws IOException {if(! file.exists()) {makeDir(file.getParentFile());}return file.createNewFile();}/*** 创建文件夹* @param dir*/public static void makeDir(File dir) {if(! dir.getParentFile().exists()) {makeDir(dir.getParentFile());}dir.mkdir();}
public static void main(String args[]) {
}//"C:/temp/a/b/c/d/e/f/g.txt";创建此目录下的g.txt文件String filePath = "C:/temp/a/b/c/d/e/f/g.txt";File localFile = new File( filePath );try {createFile(localFile);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}