File类
File类概述和构造方法
File类的概述 :文件和目录路径名的抽象表示形式
1、构造方法
publicFile(String pathname)
publicFile(String parent,String child)
publicFile(File parent,String child)
File类构造方法
27行
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
public booleancreateNewFile()
public booleanmkdir()
public booleanmkdirs()
删除功能
public booleandelete()
重命名功能
public boolean renameTo(Filedest)
创建、删除、重命名功能 41行 Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import java.io.File ;import java.io.IOException ;
/** 创建功能public boolean createNewFile()public boolean mkdir()public boolean mkdirs()删除功能public boolean delete()文件夹必须为空重命名功能public boolean renameTo(File dest)
*/public class FileDemo2 {public static void main ( String [] args ) throws IOException {// //创建功能// File file=new File("f:\\day19");// System.out.println("mkdir:"+file.mkdir());// File file2=new File("f:\\day19\\itcast");// System.out.println("mkdir:"+file2.mkdir());// //File file3=new File("F:\\day19\\itcast\\a.txt");// //System.out.println("createNewFile:"+file3.createNewFile());//// File file4=new File("f:\\day20\\itcast\\prictise\\work\\job");// File file5=new File(file4,"java.txt");// File file6=new File(file4,"aaa.txt");// System.out.println("mkdirs:"+file4.mkdirs());// System.out.println("createNewFile:"+file5.createNewFile());//// //删除功能// System.out.println("delete:"+file5.delete());// System.out.println("renameTo:"+file5.renameTo(file6));//File file7=new File("f:\\WIFIBT.pdf");//File file8=new File("f:\\WifiBt.pdf");//System.out.println("renameTo:"+file7.renameTo(file8));File file9 = new File ( "f:\\WifiBt.pdf" );File file10 = new File ( "e:\\foxconn.pdf" );System . out . println ( "renameTo:" + file9 . renameTo ( file10 ));}}4、判断功能
public booleanisDirectory()
public booleanisFile()
public booleanexists()
public booleancanRead()
public booleancanWrite()
public booleanisHidden()
判断功能 28行 Java5、基本获取功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import java.io.File ;import java.io.IOException ;
/** 判断功能public boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录public boolean isFile() 测试此抽象路径名表示的文件是否是一个标准文件public boolean exists() 测试此抽象路径名表示的文件或目录是否存在public boolean canRead() 测试应用程序是否可以读取此抽象路径名表示的文件public boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件public boolean isHidden() 测试此抽象路径名指定的文件是否是一个隐藏文件
*/public class FileDemo3 {public static void main ( String [] args ) throws IOException {//File file=new File("e:\\day0000\\itcast\\java");//System.out.println("mkdirs:"+file.mkdirs());//System.out.println("isDirectory:"+file.isDirectory());//File file2=new File("java.txt");//System.out.println("creatNewFile:"+file2.createNewFile());//System.out.println("isFile:"+file2.isFile());//File file3=new File("java.txt");//System.out.println("creatNewFile:"+file3.mkdir());//System.out.println(file3.exists());}}
获取功能:
public String getAbsolutePath():获取绝对路径
public String getPath():获取相对路径
public String getName():获取名称
public long length():获取长度。字节数public long lastModified():获取最后一次的修改时间,毫秒值
基本获取功能、高级获取功能 30行 Java练习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.io.File ;import java.text.ParseException ;import java.text.SimpleDateFormat ;import java.util.Date ;
public class FileDemo5 {public static void main ( String [] args ) throws ParseException {File file = new File ( "Demo\\java.txt" );System . out . println ( "getAbsolutePath\t" + file . getAbsolutePath ());System . out . println ( "getPath\t" + file . getPath ());System . out . println ( "getName\t" + file . getName ());System . out . println ( "length\t" + file . length ());System . out . println ( "lastModified\t" + file . lastModified ());System . out . println ( "--------------------------" );File file2 = new File ( "e:\\" );String [] astrArray = file2 . list ();//获取指定目录下的所有文件或者文件夹的名称数组for ( String s: astrArray ){System . out . println ( s );}System . out . println ( "--------------------------" );File [] fileArray = file2 . listFiles ();//获取指定目录下的所有文件或者文件夹的File数组for ( File file3: fileArray ){System . out . println ( file3 . getName ());}}}判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称
练习 36行 Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import java.io.File ;
/** 判断E盘目录下是否有后缀名为.mp3的文件,如果有,就输出此文件名称** 分析:* A:封装e判断目录* B:获取该目录下所有文件或者文件夹的File数组* C:遍历该File数组,得到每一个File对象,然后判断* D:是否是文件* 是:继续判断是否以.mp3结尾* 是:就输出该文件名称* 否:不搭理它* 否:不搭理它*/public class FileDemo {public static void main ( String [] args ) {// 封装e判断目录File file = new File ( "e:\\" );
// 获取该目录下所有文件或者文件夹的File数组File [] fileArray = file . listFiles ();
// 遍历该File数组,得到每一个File对象,然后判断for ( File f : fileArray ) {// 是否是文件if ( f . isFile ()) {// 继续判断是否以.jpg结尾if ( f . getName (). endsWith ( ".mp3" )) {// 就输出该文件名称System . out . println ( f . getName ());}}}}
文件名称过滤器 29行 Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.io.File ;import java.io.FilenameFilter ;
public class FileDemo2 {public static void main ( String [] args ) {// 封装e判断目录File file = new File ( "e:\\" );
// 获取该目录下所有文件或者文件夹的String数组// public String[] list(FilenameFilter filter)String [] strArray = file . list ( new FilenameFilter () {@Overridepublic boolean accept ( File dir , String name ) {// File file = new File(dir, name);// // System.out.println(file);// boolean flag = file.isFile();// boolean flag2 = name.endsWith(".jpg");// return flag && flag2;return new File ( dir , name ). isFile () && name . endsWith ( ".jpg" );}});
// 遍历for ( String s : strArray ) {System . out . println ( s );}}} 可}}