标签:
package cn.hello; import java.io.File; /* * IO流 * FIle类 * 构造方法: * File(String pathname) :根据路径得到File对象 * File(String pathname(目录),String chile) :根据一个目录和一个子文件(或者目录)得到File对象 * File(File parent,String chile) :根据一个File对象和一个子文件或者目录得到File对象 * * */ public class Test01 { public static void main(String[] args) { File file=new File("d:\\zf\\hello.txt"); //这个路径所指向的未必真实存在 File file2=new File("d:‘\\zf","hello.txt"); File file3=new File("d:\\zf"); File file4=new File(file3,"hello.txt"); } }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /* 7 * IO流 8 * FIle类 9 * 10 * 创建功能 11 * public boolean createNewFile() 创建文件 12 * public boolean mkdir() 创建文件夹 13 * public boolean mkdirs() 创建多级目录 14 * 15 * 删除功能 16 * public boolean delete() 17 * 重命名功能 18 * public boolean renameTo(File dest) 19 * 20 * */ 21 22 public class Test01 { 23 public static void main(String[] args) { 24 25 //创建一个文件夹,如果存在,就不创建了 26 File f1=new File("j:\\zzf"); 27 System.out.println(f1.mkdir()); 28 //试验 29 /*while(true){ 30 File f2=new File("j:\\zzf"); 31 System.out.println(f1.mkdir()); //false false false 可以得出结论,如果已经有了文件夹,再次创建就会false,即创建失败 32 }*/ 33 34 35 //创建一个文件 ,如果存在了,就不创建了 36 //必须先创建目录,才能创建文件 37 File f2=new File("j:\\zzf\\hello.txt"); 38 try { 39 System.out.println(f2.createNewFile()); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 45 46 47 //创建多级目录 48 File f4=new File("j:\\zzf\\aaa\\bbb"); 49 System.out.println(f4.mkdirs()); 50 51 } 52 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /* 7 * IO流 8 * FIle类 9 * 10 删除功能 11 * public boolean delete() 12 * 13 * 注意事项: 14 * 1:java删除不走回收站 15 * 2:如果文件夹里面有文件夹或者文件,则无法删除,必须删除内容,才可以删除目录 16 * 17 * */ 18 19 public class Test01 { 20 public static void main(String[] args) { 21 File f1=new File("j:\\zf\\aa\\bb\\cc"); 22 f1.mkdirs(); 23 File f2=new File(f1,"hello.txt"); 24 try { 25 f2.createNewFile(); 26 } catch (IOException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 31 System.out.println(f2.delete()); 32 System.out.println(f1.delete()); 33 34 } 35 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /* 7 * 重命名功能:public boolean renameTo(File dest) 8 * 9 * 路径以盘符开始:绝对路径 不以盘符开始:相对路径 10 * 11 * 路径名相同,就是改名 路径名不同,就是剪切 12 * 13 * */ 14 15 public class Test01 { 16 public static void main(String[] args) { 17 File file=new File("j:\\zf\\0001.jpg"); 18 19 File f2=new File("j:\\zf\\aa\\0002.jpg"); 20 21 System.out.println(file.renameTo(f2)); 22 } 23 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /* 7 * 判断功能 8 * public boolean isDirectory() 9 * public boolean isFile() 10 * public boolean exists() 11 * public boolean canRead() 12 * public boolean canWrite() 13 * public boolean isHidden() 14 * 15 * */ 16 17 public class Test01 { 18 public static void main(String[] args) { 19 20 } 21 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 8 /* 9 * 基本获取功能 10 * public String getAbsolutePath() 11 * public String getPath() 12 * public String getName() 13 * public long length() 文件的大小 字节数 14 * public long lastModified(0 最后一次修改的时间 15 * */ 16 17 public class Test01 { 18 public static void main(String[] args) { 19 File f1=new File("j:\\zzf\\aa"); 20 f1.mkdirs(); 21 File f2=new File(f1,"hello.txt"); 22 try { 23 f2.createNewFile(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 29 //获取绝对路径 30 System.out.println(f2.getAbsolutePath()); //j:\zzf\aa\hello.txt 31 //获取相对路径 32 System.out.println(f2.getPath()); //j:\zzf\aa\hello.txt 33 //获取名字 34 System.out.println(f2.getName()); //hello.txt 35 //获取长度 36 System.out.println(f2.length()); //0 37 //最后修改时间 38 long mt=f2.lastModified(); 39 Date d=new Date(mt); 40 41 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 42 String s=sdf.format(d); 43 System.out.println(s); 44 45 } 46 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 8 /* 9 * 判断指定目录下是否有后缀名为.jpg的文件,若有,输出名称 10 * */ 11 12 public class Test01 { 13 public static void main(String[] args) { 14 File f1=new File("j:\\"); 15 File[] arr=f1.listFiles(); 16 for(File ff:arr){ 17 if(ff.isFile()){ 18 if(ff.getName().endsWith(".jpg")){ 19 System.out.println(ff.getName()); 20 } 21 } 22 } 23 } 24 }
1 package cn.hello; 2 3 import java.io.File; 4 import java.io.FilenameFilter; 5 import java.io.IOException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 /* 10 * 判断指定目录下是否有后缀名为.jpg的文件,若有,输出名称 11 * 12 * 方式2:文件名称过滤器 FilenameFilter filter 接口 13 * public String[] list(FilenameFilter filter) 14 * public File[] listFiles(FilenameFilter filter) 15 * */ 16 17 public class Test01 { 18 public static void main(String[] args) { 19 File f1=new File("d:\\"); 20 String[] str=f1.list(new FilenameFilter() { 21 22 @Override 23 public boolean accept(File dir, String name) { 24 // TODO Auto-generated method stub 25 //return false; 26 File file=new File(dir,name); 27 boolean flag=file.isFile(); 28 boolean flag2=name.endsWith(".jpg"); 29 return flag&&flag2; 30 } 31 }); 32 33 for(String s:str){ 34 System.out.println(s); 35 } 36 } 37 }
标签:
原文地址:http://www.cnblogs.com/chengling/p/4757562.html