码迷,mamicode.com
首页 > 其他好文 > 详细

File类

时间:2017-11-26 23:02:36      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:回收站   rac   txt   write   构造   inter   绝对路径   create   from   

一、构造方法:

File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.

  1)参数里面单个路径:

    /**
     * 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);
    }

为何叫abstract pathname?
    /*
     * 传入一个绝对/相对路径
     */
    public static void demo1() {
        
        String path = "D:\\JAVA\\学习视频\\01、第一阶段java基础(27天)\\day19(异常、file)\\day19\\day19\\video\\001_今日内容.avi";技术分享图片
        String path02 = "D:\\JAVA\\学习视频\\01、第一阶段java基础(27天)\\day19(异常、file)\\day19\\day19\\video\\cao.txt";
        File file = new File(path);   //传入一个绝对路径,所得出的File对象就含有该 abstract pathname
        File file02 = new File(path02);   //传入一个绝对路径,所得出的File对象就含有该 abstract pathname
        System.out.println(file.exists());   //该绝对路径存在着该文件,所以为true
        System.out.println(file02.exists());  //该绝对路径不存在着该文件,所以为true
        
        File file2 = new File("aaa.txt");
        System.out.println(file2.exists());  //该项目下面路径存在着该文件,所以为true
        
        File file3 = new File("yyy.txt");
        System.out.println(file3.exists());  //该项目下面路径存在着该文件,所以为false
        
    }

 2)父级路径、子级路径:

public File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.

public File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
private static void demo2() {
    String parent = "D:\\JAVA\\学习视频\\01、第一阶段java基础(27天)\\day19(异常、file)\\day19\\day19\\video";
    String child = "001_今日内容.avi";
    File file = new File(parent,child);
    System.out.println(file.exists());
}

private static void demo3() {
    File parent = new File("D:\\JAVA\\学习视频\\01、第一阶段java基础(27天)\\day19(异常、file)\\day19\\day19\\video");
    String child = "001_今日内容.avi";
    File file = new File(parent, child);
    System.out.println(file.exists());
    System.out.println(parent.exists());
}

 从上图可以看出,父级路径既可以是String,也可以是File实例对象,其得到的效果是一样的

 二、一般方法

1)创建文件

public boolean createNewFile() throws IOException
    /***
     * 这个方法是创建文件,注意,无论是“yyy.txt”,还是"zzz",因为是file.createFile(),所以最终结果都是创建一个文件,是文件
     * @throws IOException
     */
    public static void demo1() throws IOException {
        File file = new File("yyy.txt");
        System.out.println(file.createNewFile());            //如果没有就创建,返回true,如果有就不创建了,返回false
        
        File file2 = new File("zzz");
        System.out.println(file2.createNewFile());
    }

2)创建文件夹

    /**
     * Creates the directory named by this abstract pathname.
     *
     * @return  <code>true</code> if and only if the directory was
     *          created; <code>false</code> otherwise*/
    public boolean mkdir() //创建文件夹 如果存在这样的文件夹,就不创建了

public boolean mkdirs() //创建文件夹,如果父文件夹不存在,会帮你创建出来
    /***
     * 这个是创建路径(文件夹)(diractory)的
     */
    private static void demo2() {
        File dir1 = new File("aaa");//创建一个文件名为aaa的文件
        System.out.println(dir1.mkdir());
        
        File dir2 = new File("bbb.txt");    
        System.out.println(dir2.mkdir());//这样写是可以的,文件夹也是可以有后缀的(创建一个文件名为bbb.txt的文件夹)
        
        File dir3 = new File("ccc\\ddd");
        System.out.println(dir3.mkdirs());//创建一个文件名问ccc的文件夹,且其子文件夹为ddd,注意,方法里面多了个“s”
    }

 

 3)重命名

public boolean renameTo(File dest)
    /***
     * 情况一:他们的路径都是一样的
     * 如果在该路径 file1 下,有一个文件zzz,那就把文件名zzz改为文件名bbb
     * 注意,如果在该路径file1下,没有该文件zzz,则无法改名,则返回false,如果重写成功则返回true
     */
    private static void demo1() {
        File file1 = new File("zzz");
        File file2 = new File("bbb");
        System.out.println(file1.renameTo(file2));
    }
    /***
     * 情况二:他们的路径不一样
     * 如果在该路径file1下,有该文件,则把该文件剪切到file2路径下,返回true
     * 如果在该路径file1下,没有该文件,则返回false
     * 如果在该路径file1下,有该文件bbb,但是file2路径下也有该文件diao,则无法剪切,返回false
     */
    private static void demo3() {
        File file1 = new File("D:\\bbb");
        File file2 = new File("diao");
        System.out.println(file1.renameTo(file2));
    }

 

 4)删除

public boolean delete()
    /***
     * Java中的删除不走回收站。
     * 要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹(也就是说必须是空的),否则删除失败并且返回false
     * 如果该目录下面有该文件,则成功删除并且返回true,如果没有的话,就返回false
     */
    private static void demo2() {
        File file1 = new File("yyy.txt");
        System.out.println(file1.delete());
    }

 

 5)判断功能

public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
public boolean setReadable(boolean readable):将其设置为是否可读
public boolean setWritable(boolean writable):将其设置为是否可写
    /***
     * windows系统认为所有的文件都是可读的,无论file.setReadable(boolean)为true还是false
     * 但是linux就不一样了
     * 但是windows系统可以设置为不可写
     */
    private static void demo2() {
        
        File file = new File("aaa.txt");
        
        file.setReadable(false);
        System.out.println(file.canRead());     
                                                    
        file.setWritable(true);
        System.out.println(file.canWrite());             

    }

 

 6)其它

    public static void demo1() throws IOException {
        
        File file1 = new File("ccc.txt");
        file1.createNewFile();
        File file2 = new File("D:\\EclipseWorkspace02\\day19_File\\ccc.txt");
        
        //获取绝对路径
        System.out.println(file1.getAbsolutePath());//(D:\EclipseWorkspace02\day19_File\ccc.txt)
        System.out.println(file2.getAbsolutePath());//(D:\EclipseWorkspace02\day19_File\ccc.txt)
        
        //获取构造方法中当时的路径
        System.out.println(file1.getPath());//ccc.txt
        System.out.println(file2.getPath());//D:\EclipseWorkspace02\day19_File\ccc.txt
        
        //获取文件或者文件的名字
        System.out.println(file1.getName());//ccc.txt
        System.out.println(file2.getName());//ccc.txt
        
        //获取文件长度
        System.out.println(file1.length());
        
        //获取该文件的最后修改时间,返回毫秒值
        Date d = new Date(file1.lastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(sdf.format(d));
        
    }

 

    private static void demo2() {
        
        File dir = new File("D:\\JAVA\\学习视频\\01、第一阶段java基础(27天)\\day19\\day19\\day19\\video");
        
        String[] arr = dir.list();//获取当前路径下的所有文件的文件名(是文件名)
        
        File[] subFiles = dir.listFiles();//获取当前路径下的所有文件的File实例(是File对象)
        
    }

 The end!

File类

标签:回收站   rac   txt   write   构造   inter   绝对路径   create   from   

原文地址:http://www.cnblogs.com/ericguoxiaofeng/p/7898636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!