IO流(File常见方法)

File类常见方法:
1,创建。
boolean createNewFile():在指定位置创建文件,如果该文件已经存在,则不创建,返回false。
和输出流不一样,输出流对象一建立创建文件,当文件已经存在则会被覆盖。
boolean mkdir():创建文件夹。(只允许创建一级文件夹)
boolean mkdirs():创建多级文件夹。

2,删除。
boolean delete();在程序发生异常时,删除会失败。
void deleteOnExit(); 在程序退出时删除指定文件。

3,判断。
boolean exists():文件是否存在。
isFile();
isDirectory();
isHidden();
isAbsolute();

4,获取信息。
getName():
getPath():
getParent()://获取父目录
getAbsolutePath() //获取绝对路径
lastModified() //最后一次修改时间 返回的是long类型
length() //返回的是long类型

import java.io.*;
class FileDemo 
{
    public static void main(String[] args) throws IOException
    {
//      consMethod();
//      method_1();
//      method_2();
//      method_3();
//      method_4();
        method_5();
    }
    public static void method_5()
    {
        File f1 = new File("E:\\Ziliao\\Java\\practice\\haha.java");
        File f2 = new File("E:\\Ziliao\\Java\\practice\\a\\heheda.txt");

        sop("rename:"+f1.renameTo(f2));
    }
    public static void method_4()
    {
        File f = new File("a.txt");

        sop("path:"+f.getPath());
        sop("abspath:"+f.getAbsolutePath());
        sop("parent:"+f.getParent()); //该方法返回的是绝对路径中的父目录。
                                    //如果获取的是相对路径,返回null。如果相对路径中有上一层目录,那么该目录就是返回结果。
    }
    public static void method_3()
    {
        File f = new File("file.txt");
        f.mkdir();

        sop("exists:"+f.exists());
        //记住:在判断文件对象是否是文件或者目录时,必须要先判断该对象封装的内容是否存在。
        //通过xitsts判断。
        sop("dir:"+f.isDirectory());
        sop("file:"+f.isFile());
        sop(f.isAbsolute());//判断是否为绝对路径
    }
    public static void method_2()
    {
        File f = new File("file.txt");

        sop("execute:"+f.canExecute());
        //创建文件夹
        File dir = new File("abc\\wzx\\xml");
//      sop("delete:"+dir.delete());
        sop("mkdir:"+dir.mkdirs());
    }
    public static void method_1() throws IOException
    {
        File f = new File("file.txt");
        f.deleteOnExit();

        sop("create:"+f.createNewFile());//创建文件
        sop("delete:"+f.delete());
        sop("exists:"+f.exists());//判断文件是否存在    
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容