- import java.io.File;
- import java.io.IOException;
- public class Test { public static void main(String[] args) {
- // File f=File(String pathname)
- File f=new File("test.txt");
- try {
- // boolean createNewFile() 如果文件不存在创建一个新的空文件, 如果存在不创建
- f.createNewFile();
- // String getAbsolutePath() // 获得文件的绝对路径
- System.out.println(f.getAbsolutePath());
- // String getName() // 获得文件名称
- System.out.println(f.getName());
- // long length() // 获得文件里面内容字节大小
- System.out.println(f.length());
- // boolean renameTo(File dest) // 重命名文件
- f.renameTo(new File("rename.txt"));
- // boolean isFile() // 判断是否是文件
- System.out.println(f.isFile());
- // boolean exists() // 判断此文件是否存在
- System.out.println(f.exists());
- // boolean delete() // 删除文件
- System.out.println(f.delete());
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2658439.html