File案例:
需求:从控制台输入一个字符串 该字符串是文件夹路径 计算这个文件夹的大小
public static File getDirFile() {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入一个文件夹路径:");
String path = scanner.nextLine();
File file = new File(path);
if (!file.exists()) {
System.out.println("你输入的路径不存在,请重新输入");
}else if (file.isFile()) {
System.out.println("你输入的是个文件,请重新输入");
}else {
return file;
}
}
}
public static long getDirLengthByFile(File file) {
long sum = 0;
File[] listFiles = file.listFiles();
for (File subF : listFiles) {
if (subF.isFile()) {
sum = sum + subF.length();
}else {
sum = sum + getDirLengthByFile(subF);
}
}
return sum;
}
public static void fun() {
File file = getDirFile();
long length = getDirLengthByFile(file);
System.out.println(length);
}
需求:从控制台输入一个字符串 该字符串是文件夹路径 删除这个文件夹
public static void deleteDir(File file) {
File[] listFiles = file.listFiles();
for (File subFile : listFiles) {
if (subFile.isFile()) {
subFile.delete();
}else {
deleteDir(subFile);
}
}
file.delete();
}
public static void fun() {
File file = getDirFile();
deleteDir(file);
}
过滤器:
是一个接口,通过定义类继承接口,规定过滤规则来对File对象进行操作
过滤规则:
过滤文件时看方法的返回值,返回true保留,返回false过滤
File中两种过滤器接口:
FlieFilter:
class MyFlieFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return false;
}
return true;
}
}
FilenameFilter:
FilenameFilter的两个参数:
1.传进来的文件夹的根路径
2.该路径下的文件名和文件夹名
class MyFileNameFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
File file = new File(dir, name);
System.out.println(file);
return false;
}
}
字节流:
参照物:该执行程序。
输出(OutPut)定义:程序到文件的输出过程
输入(InPut)定义:文件到程序的输入过程
OutputStream(字节输出流):
是个抽象类 是所有输出流的父类
OutputStream的方法:
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(49);
byte[] b = {65, 66, 67, 68};
oStream.write(b);
oStream.write(b, 1, 2);
oStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("holle\n".getBytes());
oStream.close();
}
public static void fun() {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FlieOutPutStream oStream = null;
try {
oStream = new FlieOutPutStream(file);
oStream.write("holle".getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件编写失败");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
是所有输入流的父类
注意:字节流写入的是一个字节一个字节写,读取也是一个字节一个字节的读
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println(i1);
i1 = iStream.read();
System.out.println(i1);
iStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
int read = 0;
while((read = iStream.read()) != -1) {
System.out.println((char)read);
}
iStream.close();
}
public static void fun() throws IOException {
File file = new File("/Users/lanou/Desktop/Test/jian.txt");
FileInputStream iStream = new FileInputStream(file);
byte[] b = new byte[1024];
int i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
iStream.close();
}