Java学习笔记36(File类)

时间:2023-03-08 22:31:17
Java学习笔记36(File类)

File类可以对操作系统中的文件进行操作:

File类的静态成员变量:

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
//File类的静态方法
String separator = File.pathSeparator;
System.out.println(separator);
//输出:; 目录分割符,Linux中是:
separator = File.separator;
System.out.println(separator);
//输出:\ 目录的名称分隔符,Linux中是/
}
}

File类的构造方法:

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
function1();
function2();
function3();
}
public static void function1(){
File file = new File("d:\\lol");
System.out.println(file);
//输出:d:\lol
}
public static void function2(){
File file = new File("d:","lol");
System.out.println(file);
//输出:d:\lol
}
public static void function3(){
File parent = new File("d:");
File file = new File(parent,"lol");
System.out.println(file);
//输出:d:\lol
}
}

File类的功能:

创建和删除:

package demo;

import java.io.File;
import java.io.IOException; public class FileDemo {
public static void main(String[] args) throws IOException {
function1();
function2();
function3();
} public static void function1() throws IOException {
// 创建文件,如果以及存在了,就不再创建
File file = new File("d:\\lol\\java.txt");
boolean b = file.createNewFile();
System.out.println(b);
} public static void function2() throws IOException {
// 创建单级文件夹
File file1 = new File("d:\\lol\\cs1.6");
boolean b1 = file1.mkdir();
System.out.println(b1);
// 创建多级文件夹
File file2 = new File("d:\\gta\\gta5\\game");
boolean b2 = file2.mkdirs();
System.out.println(b2);
} public static void function3() throws IOException {
// 删除,不经过回收站,直接从硬盘中删除
File file = new File("d:\\lol\\java.txt");
boolean b = file.delete();
System.out.println(b);
}
}

获取功能:

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
function1();
function2();
function3();
function4();
} public static void function1() {
File file = new File("d:\\lol\\英雄联盟\\TCLS\\Client.exe");
String name = file.getName();// 获取路径最后部分的名字
System.out.println(name);// Client.exe
} public static void function2() {
File file = new File("d:\\lol\\英雄联盟\\TCLS\\Client.exe");
long length = file.length();// 文件的字节数
System.out.println(length);//
} public static void function3() {
File file = new File("d:\\lol\\英雄联盟\\TCLS\\Client.exe");
File path = file.getAbsoluteFile();// 获取文件的绝对路径
System.out.println(path);
// 输出:d:\lol\英雄联盟\TCLS\Client.exe
} public static void function4() {
File file = new File("d:\\lol\\英雄联盟\\TCLS\\Client.exe");
File parent = file.getParentFile();// 获得父路径
System.out.println(parent);
// 输出:d:\lol\英雄联盟\TCLS
}
}

判断功能:

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
function1();
function2();
} public static void function1() {
File file = new File("d:\\lol\\英雄联盟\\cs1.6.exe");
boolean b = file.exists();// 判断文件(夹)是否存在
System.out.println(b);// false
} public static void function2() {
File file = new File("d:\\lol\\英雄联盟");
if (file.exists()) {
boolean b = file.isDirectory();// 判断是否为文件夹(路径)
System.out.println(b);// true
}
}
}

遍历目录获取(list获取):

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
function1();
function2();
} public static void function1() {
File file = new File("d:\\lol\\英雄联盟");
String[] strArr = file.list();// 获取目录下的文件以及文件夹
for (String str : strArr) {
System.out.println(str);
}
/*
输出:
7z.dll
Cross
Game
LeagueClient
SOFT_REPAIR
SpannedFileList.txt
TCLS
TQM.ini
英雄联盟.lnk
英雄联盟卸载.exe
访问官网.url
*/
} public static void function2() {
// 一样的功能,不过推荐使用这种
// 因为这种功能更强大,可以后续进行更多的操作
File file = new File("d:\\lol\\英雄联盟");
File[] fileArr = file.listFiles();
for (File f : fileArr) {
System.out.println(f);
}
/*
输出:
d:\lol\英雄联盟\7z.dll
d:\lol\英雄联盟\Cross
d:\lol\英雄联盟\Game
d:\lol\英雄联盟\LeagueClient
d:\lol\英雄联盟\SOFT_REPAIR
d:\lol\英雄联盟\SpannedFileList.txt
d:\lol\英雄联盟\TCLS
d:\lol\英雄联盟\TQM.ini
d:\lol\英雄联盟\英雄联盟.lnk
d:\lol\英雄联盟\英雄联盟卸载.exe
d:\lol\英雄联盟\访问官网.url
*/
}
}

利用递归遍历一个目录下所有文件:

package demo;

import java.io.File;

/*
* 对一个目录的下的所有内容,进行完全的遍历
* 方法的递归调用,自己调用自己
*/
public class FileDemo {
public static void main(String[] args) {
File dir = new File("d:\\lol\\英雄联盟");
getAllDir(dir);
} /*
* 定义方法,实现目录的全遍历
*/
public static void getAllDir(File dir) {
System.out.println(dir);
// 调用方法listFiles()对目录,dir进行遍历
File[] fileArr = dir.listFiles();
for (File f : fileArr) {
// 判断变量f表示的路径是不是文件夹
if (f.isDirectory()) {
// 是一个目录,就要去遍历这个目录
// 继续调用getAllDir,传递他目录
getAllDir(f);
} else {
System.out.println(f);
}
}
}
}

文件过滤器:

在遍历目录的时候,可以根据需要,只获取满足条件的文件

package demo;

import java.io.File;

public class FileDemo {
public static void main(String[] args) {
File file = new File("d:\\lol\\英雄联盟");
File[] fileArr = file.listFiles(new MyFilter());
for(File f:fileArr){
System.out.println(f);
}
}
}
//只打印者一条:d:\lol\英雄联盟\英雄联盟卸载.exe
package demo;

import java.io.File;
import java.io.FileFilter; public class MyFilter implements FileFilter {
public boolean accept(File pathname) {
// 是exe文件返回true,否则返回false
return pathname.getName().endsWith(".exe");
}
}