File类_常见的方法(获取,创建与删除,判断,重命名)

获取:
   1.1获取文本名称
   1.2获取文件路劲
   1.3获取文件大小
   1.4获取文件修改或创建时间

import java.io.File;
import java.text.DateFormat;
import java.util.Date;

public class FileGetMethodDemo {
    public static void main(String[] args) {
        getDemo();
    }

    public static void getDemo() {
        File file = new File("F:\\a.txt");
        
        //获取文本名称
        String name = file.getName();
        
        //获取文件的绝对路径
        String absPath = file.getAbsolutePath();
        
        //获取文件的相对路劲
        String path = file.getPath();
        
        //获取文件的大小
        long len = file.length();
        
        //获取文件修改时间
        long time = file.lastModified();
        
        //格式化时间
        Date date = new Date(time);
        DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        String str_time = dateformat.format(date);
        
        System.out.println("name="+name+"\nabsPath="+absPath+"\npath="+path+"\nlen="+len+"\ntime="+str_time);
    }
}

 

 

创建与删除:

  文件的创建与删除

import java.io.File;
import java.io.IOException;

public class File_CreatAndDeleteDemo {
    public static void main(String[] args) throws IOException {
        CreateAndDeleteDemo();
        
    }
    public static void CreateAndDeleteDemo() throws IOException {
        File file = new File("F:\\a.txt");
        
        //如果指定的文件不存在并成功地创建,则返回 true;如果指定的文件已经存在,则不创建,不会覆盖原有的文件返回 false 
        boolean b = file.createNewFile();
        System.out.println(b);
        
        boolean d = file.delete();
        System.out.println(d);
    }
}

 

  文件夹的创建与删除

import java.io.File;
import java.io.IOException;

public class File_CreatAndDeleteDemo {
    public static void main(String[] args) throws IOException {
        CreateAndDeleteDemo();
        
    }
    public static void CreateAndDeleteDemo() {
        File dir = new File("F:\\abc");
        
        //File dir = new File("F:\\abc\\b\\c\\d\\e\\f\\c");
        //boolean b = dir.mkdirs();//创建多级目的,删除的话,只能删掉最里面的那个目录
        
        
        boolean b = dir.mkdir();//只能创建一级目录
        System.out.println(b);
        
        //boolean d = dir.delete();//如果要删除的文件不为空,则删除不成功
        //System.out.println(d);
    }
}

 

 

 

判断

import java.io.File;

public class File_isDemo {
    public static void main(String[] args) {
        isDemo();
    }
    
    public static void isDemo(){
        File file = new File("F:\\a.txt");
        
        file.mkdir();
        
        //最好先判断文件是否存在,可以用于在删除的时候先判断一下文件是否存在,因为有可能文件正在被操作的时候是删除不了的,因为删除调用的是windows底层的方法
        boolean b = file.exists();
        System.out.println(b);
        //判断是否是文件,如果文件不存在为false
        System.out.println(file.isFile());
        //判断是否是目录
        System.out.println(file.isDirectory());
    }
}

 

 重命名:

import java.io.File;

public class File_RenameTo {
    public static void main(String[] args) {
        RenameTo();
    }
    //重命名还可以用于剪切文件
    private static void RenameTo() {
        File file1 = new File("F:\\42-IO流(Proes集合的-基本功能).avi");
        File file2 = new File("E:\\huangjianfeng.avi");
        boolean b = file1.renameTo(file2);
        System.out.println(b);
    }
}

 

posted @ 2014-03-11 17:03  胡椒粉hjf  阅读(417)  评论(0编辑  收藏  举报