Java_File

总述:可以实现文件和文件夹的增删,对文件内容无效

An abstract representation of file and directory pathnames.

StringgetName()
Returns the name of the file or directory denoted by this abstract pathname.

返回获取文件或文件夹的名

booleanisFile()
Tests whether the file denoted by this abstract pathname is a normal file.

判断他是否是一个文

booleanmkdir()
Creates the directory named by this abstract pathname.

创建文件夹的名

booleanexists()
Tests whether the file or directory denoted by this abstract pathname exists.

判断这个文件或文件夹是否存

下面这个代码请不要轻易实践,确保你的./a没写成.

public class Test {
	public static void main(String[] args) {
		File dir = new File("./a");
		delete(dir);
	}
	/**
	 * 将给定的File表示的文件或目录删除
	 */
	public static void delete(File file) {
		if(file.isDirectory()) {
			//先将目录清空
			File[] subs = file.listFiles();
			for (int i = 0; i < subs.length; i++) {
				File sub  = subs[i];
				delete(sub);
			}
		}
		file.delete();
		
	}
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页