File类中的一些方法
package com.xiao.filedemo; import org.junit.Test; import java.io.File; import java.io.IOException; /** * @Author 笑笑 * @Date 20:46 2018/04/25 */ public class FileDemo_05 { //boolean createNewFile() 当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。文件如果存在,不创建 //返回boolean值 @Test public void test_01() throws IOException { File file = new File("c:\\a.txt"); boolean b = file.createNewFile(); System.out.println(b); } //boolean mkdir()创建此抽象路径名指定的目录。 @Test public void test_02() throws IOException { File file = new File("c:\\新建文件夹"); boolean b = file.mkdir(); System.out.println(b); } //boolean mkdirs() 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。 @Test public void test_03() throws IOException { File file = new File("c:\\新建文件夹\\abc"); boolean b = file.mkdirs(); System.out.println(b); } //boolean delete() 删除此抽象路径名表示的文件或目录(慎重!不走回收站) @Test public void test_04() throws IOException { File file = new File("c:\\新建文件夹\\abc"); boolean b = file.delete(); System.out.println(b); } //String getName() 返回由此抽象路径名表示的文件或目录的名称。 @Test public void test_05() throws IOException { File file = new File("c:\\新建文件夹\\abc"); String name = file.getName(); //打印结果为abc System.out.println(name); } // String getPath() 将此抽象路径名转换为一个路径名字符串。 @Test public void test_06() throws IOException { File file = new File("c:\\新建文件夹\\abc"); String name = file.getPath(); //打印结果为 c:\新建文件夹\abc System.out.println(name); } //long length() 返回由此抽象路径名表示的文件的长度(大小,单位:字节) @Test public void test_07() throws IOException { File file = new File("c:\\新建文件夹\\abc"); long length = file.length(); //打印结果为 0 System.out.println(length); } //File getAbsoluteFile() String getAbsolutePath() @Test public void test_08() throws IOException { File file = new File("src"); // String path = file.getAbsolutePath(); File f = file.getAbsoluteFile(); //打印结果为C:\Users\Administrator\Desktop\Java_Idea\src System.out.println(f); } //boolean exists() 测试此抽象路径名表示的文件或目录是否存在 @Test public void test_09() throws IOException { File file = new File("c:\\新建文件夹\\abc"); boolean b = file.exists(); System.out.println(b); } //boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录。(目录必须存在,不存在直接返回fasle) @Test public void test_10() throws IOException { File file = new File("c:\\新建文件夹\\abc"); boolean b = file.isDirectory(); System.out.println(b); } //boolean isFile() 测试此抽象路径名表示的文件是否是一个标准文件。(文件必须存在,不存在直接返回fasle) @Test public void test_11() throws IOException { File file = new File("c:\\新建文件夹\\abc\\1.txt"); boolean b = file.isFile(); System.out.println(b); } //String[] list() 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。(遍历目录下所有文件、文件夹,包括隐藏和受保护的文件) @Test public void test_12() throws IOException { File file = new File("c:\\新建文件夹"); String[] list = file.list(); //打印结果为abc for (String s: list) { System.out.println(s); } } //File[] listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。(遍历目录下所有文件、文件夹,包括隐藏和受保护的文件,返回每个文件、文件夹的全路径的文件对象) @Test public void test_13() throws IOException { File file = new File("c:\\新建文件夹"); File[] files = file.listFiles(); //打印结果为 c:\新建文件夹\abc for ( File f: files) { System.out.println(f); } } //static File[] listRoots() 列出可用的文件系统根。(系统所有的根目录) @Test public void test_14() throws IOException { File[] files = File.listRoots(); for ( File f: files) { System.out.println(f); } } }