标签:
1 public class Test1 { 2 public static void main(String[] args) { 3 String path; 4 5 path = "D:/eclipse/eclipse.exe"; 6 //path = "D:/eclipse/"; 7 //path = "d:/4rtg34t54545454y45t"; 8 9 //创建File对象,封装一个磁盘路径 10 File f = new File(path); 11 System.out.println("可读: "+f.canRead()); 12 System.out.println("可写: "+f.canWrite()); 13 System.out.println("可执行: "+f.canExecute()); 14 System.out.println("是否隐藏: "+f.isHidden()); 15 System.out.println("是否存在: "+f.exists()); 16 System.out.println("完整路径: "+f.getAbsolutePath()); 17 System.out.println("文件名: "+f.getName()); 18 System.out.println("父目录: "+f.getParent()); 19 System.out.println("最后修改时间: "+f.lastModified()); 20 System.out.println("字节量(目录无效): "+f.length()); 21 System.out.println("是否目录: "+f.isDirectory()); 22 System.out.println("是否文件: "+f.isFile()); 23 System.out.println("总空间: "+f.getTotalSpace()); 24 System.out.println("可用空间: "+f.getFreeSpace()); 25 } 26 }
1 public class Test2 { 2 public static void main(String[] args) throws IOException { 3 //封装路径 4 File f = new File("d:/abc/f1"); 5 //判断这个路径是否存在 6 if(f.exists()) {//已经存在 7 boolean b = f.delete();//删除 8 System.out.println( 9 "删除是否成功:"+b); 10 } else {//不存在 11 //创建文件 12 /* 13 * 1. 所在的目录不存在,会出现异常 14 * 2. 文件已经存在,会返回 false 15 */ 16 boolean b = f.createNewFile(); 17 System.out.println( 18 "创建是否成功:"+b); 19 } 20 } 21 }
1 public class Test3 { 2 public static void main(String[] args) { 3 File f = new File( 4 "d:/abc/aa/bb/cc/dd/"); 5 if(f.exists()) { 6 boolean b = f.delete(); 7 System.out.println( 8 "删除目录是否成功:"+b); 9 } else { 10 //mkdirs() 创建多层目录 11 boolean b = f.mkdirs(); 12 System.out.println( 13 "创建目录是否成功:"+b); 14 } 15 } 16 }
1 public class Test4 { 2 public static void main(String[] args) { 3 File f1 = new File("d:/abc/a.jpg"); 4 File f2 = new File("d:/abc/a_copy.jpg"); 5 if(! (f1.exists() ^ f2.exists())) { 6 System.out.println( 7 "两个路径不能同时存在,或同时不存在"); 8 return; 9 } 10 if(f1.exists()) { 11 boolean b = f1.renameTo(f2); 12 System.out.println("改名是否成功:"+b); 13 } else { 14 boolean b = f2.renameTo(f1); 15 System.out.println("改名是否成功:"+b); 16 } 17 } 18 }
1 public class Test5 { 2 public static void main(String[] args) throws IOException { 3 /* 4 * 在系统临时目录下, 5 * 创建临时文件, 6 * 并将该文件的路径, 7 * 封装成 File 对象返回 8 */ 9 File f = 10 File.createTempFile( 11 "abc", ".txt"); 12 13 System.out.println( 14 f.getAbsolutePath()); 15 } 16 }
1 public class Test6 { 2 public static void main(String[] args) { 3 File dir = 4 new File("c:/windows/"); 5 6 String[] names = dir.list(); 7 for(String s : names) { 8 System.out.println(s); 9 } 10 System.out.println("----------------------"); 11 File[] files = dir.listFiles(); 12 for(File f : files) { 13 System.out.println( 14 f.getName()+" - "+f.length()); 15 } 16 } 17 }
1 package Mytest; 2 3 import java.io.File; 4 import java.io.FileFilter; 5 import java.io.FilenameFilter; 6 7 public class test1 { 8 public static void main(String[] args) { 9 File dir = new File( 10 "c:/windows"); 11 12 String[] names = dir.list(new FilenameFilter() { 13 @Override 14 public boolean accept( 15 File dir,//正在列表的目录 16 String name) {//其中一个文件名 17 //name以".exe"结尾 18 return name.toLowerCase() 19 .endsWith(".exe"); 20 } 21 }); 22 23 24 for (String s : names) { 25 System.out.println(s); 26 } 27 System.out.println("=================================="); 28 29 30 File[] files = dir.listFiles(new FileFilter() { 31 @Override 32 public boolean accept(File f) { 33 if(f.isDirectory()) { 34 return false; 35 } 36 37 return f.length()>1024*1024; 38 } 39 }); 40 41 42 for (File f : files) { 43 System.out.println( 44 f.getName()+" - "+f.length()); 45 } 46 } 47 }
test8运行结果:
bfsvc.exe
explorer.exe
HelpPane.exe
hh.exe
notepad.exe
py.exe
pyw.exe
regedit.exe
splwow64.exe
winhlp32.exe
write.exe
xinstaller.1.3.0.22.exe
==================================
explorer.exe - 4502352
MEMORY.DMP - 530793625
MFGSTAT.zip - 1926298
RtlExUpd.dll - 2080472
标签:
原文地址:http://www.cnblogs.com/fazheng/p/5324607.html