File类
- File类代表与平台无关的文件和目录。
- FIle能创建、删除、重命名文件和目录,但FIle不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入流/输出流。
- 访问文件名:
- getName();
- getPath();
- getAbsoluteFile();
- getAbsolutePath();
- getParent();
- renameTo(File newName);
- 文件检测:
- exists();
- canWrite();
- canRead();
- isFile();
- isDirectory();
- 获取常规文件信息
- 文件操作相关
- createNewFile();
- delete();
- 目录操作相关
- mkDir();
- list();
- listFiles();
示例代码
package test.com.atguigu.javase.lesson10;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class IOTestTest {
@Test
public void testFile() throws IOException {
File file = new File("hello.txt");
String fileName = file.getName();
System.out.println(fileName);
String path = file.getAbsolutePath();
System.out.println(path);
System.out.println(file.exists());
File dir = new File("atguigu");
System.out.println(dir.isDirectory());
System.out.println(dir.isFile());
System.out.println(file.length());
File file2 = new File("abcd.txt");
file2.createNewFile();
}
}