码迷,mamicode.com
首页 > 编程语言 > 详细

java file

时间:2020-07-12 12:26:01      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:some   jpg   tac   cto   data   tde   efi   sys   getpath   

技术图片

 @Test
    public void test1(){
        File file1 = new File("/Users/truman/Desktop/Life");
        System.out.println(file1);
        System.out.println(file1.isDirectory());

        File file2 = new File("/Users/truman", "Pictures");
        System.out.println(file2);
        System.out.println(file2.isDirectory());

        File file3 = new File(file2, "aiaiai.jpg");
        System.out.println(file3);
    }

Common Methods

技术图片

String getAbsolutePath()

String getPath()

String getName()

String getParent()

long length()

long lastModified()

@Test
    public void testFileMethod(){
        File file1 = new File("/Users/truman/Desktop");

        System.out.println(file1.getAbsolutePath()); // /Users/truman/Desktop
        System.out.println(file1.getPath());  // /Users/truman/Desktop
        System.out.println(file1.getName()); // Desktop
        System.out.println(file1.getParent()); // /Users/truman
        System.out.println(file1.length());  // 672
        System.out.println(file1.lastModified());  // 1593138078000  
        System.out.println(new Date(file1.lastModified()));  // Fri Jun 26 10:21:18 CST 2020
        
    }

String[] list()

String[] list = file1.list();
        System.out.println(Arrays.toString(list));
// [Villanelle, Database, 数学建模书籍, .DS_Store, Something, .localized, ...]

FIle[] listFiles()

File[] files = file1.listFiles();

boolean renameTo(File file) (实际上是移动文件)

技术图片

More Methods

技术图片


技术图片

boolean createNewFile()

@Test
    public void testCreateFile(){
        File file1 = new File("hi.txt");
        if(!file1.exists()){
            try {
                file1.createNewFile();
                System.out.println("New File Created.");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

boolean delete()

   @Test
    public void testDelete(){
        File file1 = new File("hi.txt");
        if (file1.exists()){
            file1.delete();
            System.out.println("File Deleted.");
        }
    }
}

java file

标签:some   jpg   tac   cto   data   tde   efi   sys   getpath   

原文地址:https://www.cnblogs.com/nedrain/p/13287804.html

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