java学习之File类--2015-2-26

来源:互联网 发布:mac放电影有杂音 编辑:程序博客网 时间:2024/06/05 21:54

File类

解释:文件和目录路径名的抽象表示形式,建立File对象不会对文件系统产生影响。

 

程序-----file-------》文件/文件夹:通过File建立联系,而并不是读写文件操作,真正操作内容是通过IO流操作。

 

1.      两个常量

(1)      路径分隔符pathSeparator

(2)      名称分隔符 \(windows) /(linux)等。Separator

  

public class Demo01 {public static void main(String[] args){System.out.println(File.pathSeparator);//输出:;System.out.println(File.separator);//输出:\//路径表示形式String path ="E:\\xp\\test\\2.jpg";path = "E:"+File.separator+"xp"+File.separator+"test"+File.separator+"2.jpg";System.out.println(path);//推荐方式path = "E:/xp/test/2.jpg";}}

2.      相对路径和绝对路径构造File对象

1.相对路径

 *   File(String parent,String child) ==File("E:/xp/test",2.jpg);

 *   File(File(parentd),String child) ==File(new File("E:/xp/test"),"2.jpg");

 * 2.绝对路径

 *   File(String name);

 

public class Demo02 {public static void main(String[] args){String parentPath="E:/xp/test";String name ="2.jpg";//相对路径File src = new File(parentPath,name);src = new File(new File(parentPath),name);//输出System.out.println(src.getName());//2.jpgSystem.out.println(src.getPath());//E:\xp\test\2.jpg//绝对路径路径src = new File("E:/xp/test/2.jpg");System.out.println(src.getName());System.out.println(src.getPath());//没有盘符:以 user.dir 的路径构建src = new File("test.txt");System.out.println(src.getName());System.out.println(src.getPath());System.out.println(src.getAbsolutePath());//E:\workspace\IO\test.txt}}


3.      常用方法

1、文件名

getName()文件名、路径名

getPath()路径名

getAbsoluteFile()绝对路径所对应的File对象

getAbsolutePath()绝对路径名

getParent()父目录 ,相对路径的父目录,可能为null 如. 删除本身后的结果

2、判断信息

exists()

canWrite()

canRead()

isFile()

isDirectory()

isAbsolute():消除平台差异,ie以盘符开头,其他以/开头

3、长度 字节数  不能读取文件夹的长度

length()

4、创建、删除

createNewFile()不存在创建新文件,存在返回false

delete() 删除文件

staticcreateTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间

staticcreateTempFile(前缀3个字节长,后缀默认.temp,目录)

deleteOnExit() 退出虚拟机删除,常用于删除临时文件

 5、操作目录

mkdir() 创建目录,必须确保 父目录存在,如果不存在,创建失败

mkdirs() 创建目录,如果父目录链不存在一同创建

list() 文件|目录 名字符串形式

listFiles()

static listRoots() 根路径

public class Demo03 {public static void main(String[] args){//test1();//test2();//try {//test3();//} catch (IOException e) {//e.printStackTrace();//} catch (InterruptedException e) {//e.printStackTrace();//}test5();}//子目录或者文件public static void test5(){String path = "E:/workspace/images";File src = new File(path);//文件夹if(src.isDirectory()){System.out.println("==子目录|文件名==");String[] listname = src.list();for(String temp:listname)System.out.println(temp);}System.out.println("==子目录|文件File==");File[] listfile = src.listFiles();//完整路径for(File file:listfile){System.out.println(file.getAbsolutePath());}System.out.println("==过滤子文件==");File[] listfiles = src.listFiles(new FilenameFilter(){//内部类@Overridepublic boolean accept(File dir, String name) {//System.out.println(dir.getAbsolutePath());return new File(dir,name).isFile() && name.endsWith(".jpg");}});for(File file:listfiles){System.out.println(file.getAbsolutePath());}}//操作目录public static void test4(){String path = "E:/workspace/images/a";File src = new File(path);//文件夹src.mkdir();//创建目录,必须确保 父目录存在,如果不存在,创建失败src.mkdirs();//创建目录,如果父目录链不存在一同创建}//创建删除文件public static void test3() throws IOException, InterruptedException{//createNewFile() 不存在创建新文件String path = "E:/workspace/images/1.jpg";File src = new File(path);if(!src.exists()){boolean flag = src.createNewFile();//存在创建失败,返回falseSystem.out.println(flag?"成功":"失败");}//删除文件boolean flag = src.delete();System.out.println(flag?"成功":"失败");//static createTempFile(前缀3个字节长,后缀默认.temp) 默认临时空间//staticcreateTempFile(前缀3个字节长,后缀默认.temp,目录)//deleteOnExit() 退出虚拟机删除,常用于删除临时文件File temp = File.createTempFile("test", ".temp",new File("E:/workspace/images"));Thread.sleep(10000);temp.deleteOnExit();}//判断信息public static void  test2(){//String path = "bg.jpg";String path = "E:/workspace/images/bg.jpg";File src = new File(path);//是否存在System.out.println("文件是否存在"+src.exists());//是否可读写 canWrite() canRead();System.out.println("文件是否可写"+src.canWrite());//ifFile();//isDirectory()if(src.isFile()){System.out.println("文件");}else if(src.isDirectory()){System.out.println("文件夹");}else{System.out.println("文件不存在");}System.out.println("字节"+src.length());}//文件名public static void test1(){//File src =  new File("E:/workspace/images/bg.jpg");File src =  new File("bg.jpg");System.out.println(src.getName());//返回名称System.out.println(src.getPath());//如果是绝对路径返回完整路径,否则相对路径System.out.println(src.getAbsolutePath());//返回完整路径System.out.println(src.getParent());//返回上一级目录,如果是相对,则返回null}}

4. 输出子孙级目录|文件的名称(绝对路径)

 * 1、listFiles()

 * 2、递归

 * static listRoots() 根路径

public class Demo04 {public static void main(String[] args) {String path = "E:/workspace/images";File file = new File(path);//printname(file);File[] roots = file.listRoots();//输出盘符System.out.println(Arrays.toString(roots));}public static void printname(File dir){if(null == dir || !dir.exists()){return ;}System.out.println(dir.getAbsolutePath());if(dir.isDirectory()){for(File sub:dir.listFiles())printname(sub);}}}


 

0 0
原创粉丝点击