目录
File类
1. File类的作用
2. File类的使用
常用构造方法
示例
常用方法
1.获取文件相关信息
示例
2.文件相关的判断
示例
3.文件列表相关
示例
3. 递归
示例
1.使用递归求1~100的累加和。
2.使用递归打印文件夹下所有文件信息
练习
思考:如何删除一个文件夹?
Java SE文章参考:Java SE入门及基础知识合集-CSDN博客
File类
1. File类的作用
java.io.File 类是对存储在磁盘上的文件信息的一个抽象表示。主要用于文件的创建、查找和删除。
2. File类的使用
常用构造方法
public File ( String pathname ); // 通过将给定的字符串路径名转换为抽象路径名来创建 File 实例public File ( String parent , String child ); // 通过给定的字符父级串路径和字符串子级路径来创建File 实例public File ( File parent , String child ); // 通过父级抽象路径名和字符串子路径创建 File 实例。
示例
package com . we . file ;import java . io . File ;/*** File 类构造方法的使用*/public class Example1 {public static void main ( String [] args ) {File file1 = new File ( "F:\\a\\b\\c.txt" );File file2 = new File ( "F:\\a\\b" , "c.txt" );File parent = new File ( "F:\\a\\b" );File file3 = new File ( parent , "c.txt" );}}
常用方法
1.获取文件相关信息
// 绝对路径:带有盘符的路径称之为绝对路径// 相对路径:不带盘符的路径称之为相对路径,相对路径相对于当前工程来定位的。public String getAbsolutePath (); // 获取文件的绝对路径public String getName (); // 获取文件的名字public String getPath (); // 获取文件的路径public File getParentFile (); // 获取文件的父文件public String getParent (); // 获取文件的父文件路径public long length (); // 获取文件的大小public long lastModified (); // 获取文件最后修改时间
示例
package com . we . file ;import java . io . File ;public class Example2 {public static void main ( String [] args ) {File file = new File ( "F:\\a\\b\\c.txt" );//获取文件的绝对路径String absPath = file . getAbsolutePath ();System . out . println ( absPath );//获取文件的路径,可能是相对路径,也可能是绝对路径String path = file . getPath ();System . out . println ( path );String name = file . getName (); // 获取文件名System . out . println ( name );//获取文件的父级文件夹对象File parentFile = file . getParentFile ();System . out . println ( parentFile . getPath ());//获取文件的父级路径String parentPath = file . getParent ();System . out . println ( parentPath );//获取文件的大小,单位是字节long length = file . length ();System . out . println ( length );//获取文件的最后修改时间long lastUpdateTime = file . lastModified ();System . out . println ( lastUpdateTime );//获取系统当前时间:单位毫秒long currentTime = System . currentTimeMillis ();System . out . println ( currentTime );File file1 = new File ( "chapter16\\c.txt" );System . out . println ( file1 . getAbsolutePath ());System . out . println ( file1 . getPath ());}}
2.文件相关的判断
public boolean canRead (); // 是否可读public boolean canWrite (); // 是否可写public boolean exists (); // 是否存在public boolean isDirectory (); // 是否是目录public boolean isFile (); // 是否是一个正常的文件public boolean isHidden (); // 是否隐藏public boolean canExecute (); // 是否可执行public boolean createNewFile () throws IOException ; // 创建新的文件public boolean delete (); // 删除文件public boolean mkdir (); // 创建目录,一级public boolean mkdirs (); // 创建目录,多级public boolean renameTo ( File dest ); // 文件重命名
示例
package com . we . file ;import java . io . File ;import java . io . IOException ;/*** 文件判断*/public class Example3 {public static void main ( String [] args ) {File file = new File ( "F:\\record\\stu.txt" );//判断文件是否可读boolean readable = file . canRead ();System . out . println ( " 文件是否可读: " + readable );//判断文件是否可写boolean writable = file . canWrite ();System . out . println ( " 文件是否可写: " + writable );//判断文件是否存在boolean exists = file . exists ();System . out . println ( " 文件是否存在: " + exists );//判断文件是否是目录boolean isDirectory = file . isDirectory ();System . out . println ( " 文件是否是目录: " + isDirectory );File parent = file . getParentFile ();System . out . println ( " 父级文件是否是目录: " + parent . isDirectory ());//判断文件是否是隐藏文件boolean hidden = file . isHidden ();System . out . println ( " 文件是否是隐藏文件: " + hidden );//判断文件是否可执行boolean executable = file . canExecute ();//所谓的可执行文件,是指双击之后有反应的文件都称之为可执行文件System . out . println ( " 文件是否可执行: " + executable );File newFile = new File ( "F:\\test\\stu\\new.txt" );File parentFile = newFile . getParentFile ();if ( ! parentFile . exists ()){ // 通常会与创建目录的方法配合使用//创建父级目录,但只能创建一级// parentFile.mkdir();//创建多级父级目录parentFile . mkdirs ();}if ( ! newFile . exists ()){try {//创建文件时,必须保证改文件的父级目录存在,否则,创建将报IO异常boolean success = newFile . createNewFile ();System . out . println ( " 文件创建是否成功: " + success );} catch ( IOException e ) {e . printStackTrace ();}}boolean deleteSuccess = file . delete ();System . out . println ( " 文件删除是否成功: " + deleteSuccess );//删除文件夹时,必须保证文件夹中没有任何文件,也就是保证文件夹是空的boolean deleteFolderSuccess = parentFile . delete ();System . out . println ( " 文件夹删除是否成功: " + deleteFolderSuccess );File renameDest = new File ( "F:\\test\\a.txt" );//文件重命名至目标文件夹时,必须保证目标文件夹存在。重命名操作成功后,原来的文件就移动过去了。boolean renameSuccess = newFile . renameTo ( renameDest );System . out . println ( " 文件重命名是否成功: " + renameSuccess );}}
删除文件夹时必须保证文件夹为空,否则将删除失败
3.文件列表相关
public File [] listFiles (); // 列出文件夹下所有文件public File [] listFiles ( FileFilter filter ); // 列出文件夹下所有满足条件的文件
示例
package com . we . file ;import java . io . File ;import java . io . FileFilter ;/*** 文件列表*/public class Example4 {public static void main ( String [] args ) {File directory = new File ( "F:\\study\\java" );//列出文件夹中所有文件File [] files = directory . listFiles ();//需要做非空判断,因为目录可能是非法的,也就是可能不存在if ( files != null ){// for(int i=0; i<files.length; i++){// File file = files[i];// }// for(File file: files){//增强for循环// System.out.println(file.getPath());// }}File folder = new File ( "F:\\Idea\\IntelliJ IDEA 2020.1\\bin" );// class A implements FileFilter{// @Override// public boolean accept(File pathname) {// return false;// }// }//匿名内部类 => 相当于将类的名字隐藏起来FileFilter filter = new FileFilter () {//表示接受文件的条件@Overridepublic boolean accept ( File file ) {String name = file . getName (); // 获取文件名,也包含后缀在内//返回文件名是否以.ext结尾return name . endsWith ( ".exe" ); //startsWith("hello") 检测字符串是否hello 开始}};File [] childFiles = folder . listFiles ( filter );if ( childFiles != null ){for ( File file : childFiles ){System . out . println ( file . getPath ());}}}}
3. 递归
- 在方法内部再调用自身就是递归。递归分为直接递归和间接递归。
- 直接递归就是方法自己调用自己。
- 间接递归就是多个方法之间相互调用,形成一个闭环,从而构成递归。
- 使用递归时必须要有出口,也就是使递归停下来。否则,将导致栈内存溢出。
package com . we . file ;import java . util . Scanner ;/*** 递归*/public class Example5 {private static Scanner sc = new Scanner ( System . in );public static void main ( String [] args ) {showMenu ();// gotoLogin();}public static void showMenu (){ // 递归没有出口,将导致栈内存溢出showMenu ();}public static void gotoLogin (){ // 间接递归System . out . println ( " 登录 " );System . out . println ( " 请输入菜单编号: " );int number = sc . nextInt ();if ( number == 1 ){gotoMain ();} else {System . out . println ( " 感谢使用 XXX 系统 " );}}public static void gotoMain (){ // 间接递归System . out . println ( " 主菜单 " );System . out . println ( " 请输入菜单编号: " );int number = sc . nextInt ();if ( number == 5 ){gotoLogin ();} else {System . out . println ( " 你选择了其他菜单 " );}}}
示例
1.使用递归求1~100的累加和。
package com . we . file ;/*** 使用递归求 1~100 的累加和。*/public class Example6 {public static void main ( String [] args ) {int result = sum ( 100 );System . out . println ( result );}public static int sum ( int number ){if ( number == 1 ) return 1 ;return number + sum ( number - 1 );}}
2.使用递归打印文件夹下所有文件信息
// 递归遍历文件夹public static void recursiveFolder ( File folder ){if ( folder . isDirectory ()){ // 检测是否是文件夹File [] files = folder . listFiles ();for ( File file : files ){if ( file . isDirectory ()){ // 如果是文件夹,就再调用方法进行查看recursiveFolder ( file );} else {System . out . println ( file . getPath ());}}} else { // 不是文件夹就直接打印文件的路径System . out . println ( folder . getPath ());}}
练习
使用递归求 6 的阶乘。
public static int multiply ( int number ){if ( number == 0 || number == 1 ) return 1 ;return number * multiply ( number - 1 );}
思考:如何删除一个文件夹?
public static void deleteFolder ( File folder ){if ( folder . isDirectory ()){ // 是文件夹就需要再进去看File [] files = folder . listFiles ();if ( files != null ){for ( File file : files ){if ( file . isDirectory ()){deleteFolder ( file );} else {file . delete ();}}folder . delete (); // 文件夹中文件删除完毕之后,文件夹也需要删除掉}} else { // 不是文件夹,直接删除folder . delete ();}}