递归一定要有条件限制,保证其能够停下来,防止发生栈内存溢出;
递归的次数不能太多,否则也会发生栈内存溢出;
构造方法,禁止递归。
1、File 静态成员变量
package mypackage.day15.demo01;
import java.io.File;
public class FileStaticVar {
public static void main(String[] args) {
System.out.println(File.separator);
System.out.println(File.separatorChar);
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);
System.out.println("C:"+File.separator+"a.txt");
}
}
2、File构造方法
package mypackage.day15.demo01;
import java.io.File;
public class FileConstruct {
public static void main(String[] args) {
construct1();
construct2();
construct3();
}
private static void construct3() {
File subfile = new File("D:\\Aplication\\WeChat\\StoreFile");
File file = new File(subfile, "WeChat Files\\wxid_nqv5pfqxwpdh21\\FileStorage\\File\\2020-10");
System.out.println(file);
}
private static void construct2() {
File file = new File("D:\\Aplication\\WeChat\\StoreFile",
"WeChat Files\\wxid_nqv5pfqxwpdh21\\FileStorage\\File\\2020-10");
System.out.println(file);
}
private static void construct1() {
File file = new File("D:\\Aplication\\WeChat\\StoreFile" +
"\\WeChat Files\\wxid_nqv5pfqxwpdh21\\FileStorage\\File\\2020-10");
System.out.println(file);
}
}
3、 File成员方法
package mypackage.day15.demo02;
import java.io.File;
import java.io.IOException;
public class FileMemberMethod {
public static void main(String[] args) throws IOException {
File file = new File("D:\\Aplication\\WeChat\\StoreFile");
System.out.println("====get====");
mem1(file);
System.out.println("====judge====");
mem2(file);
System.out.println("====create and delete====");
mem3(file);
System.out.println("====look====");
mem4();
}
private static void mem4() {
File file = new File("D:\\Aplication\\WeChat\\StoreFile\\WeChat Files\\All Users");
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
File[] files = file.listFiles();
for (File file1 : files) {
System.out.println(file1);
}
}
private static void mem3(File file) throws IOException {
File file1 = new File(file, "\\test\\abc");
System.out.println(file1.mkdirs());
System.out.println(file1.mkdir());
System.out.println(file1.createNewFile());
System.out.println(file1.delete());
}
private static void mem2(File file) {
System.out.println(file.exists());
System.out.println(file.isDirectory());
System.out.println(file.isFile());
}
private static void mem1(File file) {
System.out.println(file.getAbsolutePath());
System.out.println(file.getAbsoluteFile());
System.out.println(file.getPath());
System.out.println(file.getName());
System.out.println(file.length());
}
}
package mypackage.day15.demo03;
import java.io.File;
import java.io.IOException;
public class RecursionExecise1 {
public static void main(String[] args) {
int sum = sum(100);
System.out.println(sum);
}
private static int sum(int i) {
if (i==1){
return 1;
}
return i+sum(i-1);
}
}
5、练习:使用递归计算n的阶乘
package mypackage.day15.demo03;
public class RecursionExecise2 {
public static void main(String[] args) {
int jiecheng = jc(5);
System.out.println(jiecheng);
}
private static int jc(int i) {
if (i==1){
return 1;
}
return i*jc(i-1);
}
}
6、练习:递归打印多级目录
package mypackage.day15.demo03;
import java.io.File;
public class RecursionExecise3 {
public static void main(String[] args) {
File file = new File("D:\\Aplication\\WeChat\\StoreFile\\WeChat Files\\All Users");
getFilesName(file);
}
private static void getFilesName(File file) {
System.out.println(file);
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
getFilesName(file1);
}else {
System.out.println(file1);
}
}
}
}
}
7、练习:文件搜索
package mypackage.day15.demo03;
import java.io.File;
public class RecursionExecise4 {
public static void main(String[] args) {
File file = new File("D:\\Aplication\\WeChat\\StoreFile\\WeChat Files\\All Users");
findFilesName(file);
}
private static void findFilesName(File file) {
if (file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
findFilesName(file1);
}else {
if (file1.toString().endsWith(".jpg")){
System.out.println(file1);
}
}
}
}
}
}
8、过滤器的使用
package mypackage.day15.demo03;
import java.io.File;
import java.io.FileFilter;
public class RecursionExecise5 {
public static void main(String[] args) {
File file = new File("D:\\Aplication\\WeChat\\StoreFile\\WeChat Files\\All Users");
findFilesName(file);
}
private static void findFilesName(File file) {
File[] files1 = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()){
findFilesName(pathname);
return false;
}else {
if (pathname.toString().endsWith(".jpg")){
return true;
}
return false;
}
}
});
for (File file1 : files1) {
System.out.println(file1);
}
}
}
9、练习:用过滤器进行文件搜索
package mypackage.day15.demo03;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
public class RecursionExecise6 {
public static void main(String[] args) {
File file = new File("D:\\Aplication\\WeChat\\StoreFile\\WeChat Files\\All Users");
findFilesName(file);
}
private static void findFilesName(File file) {
// File[] files1 = file.listFiles(new FilenameFilter() {
// @Override
// public boolean accept(File dir, String name) {
// File pathname = new File(dir, name);
// if (pathname.isDirectory()){
// findFilesName(pathname);
// return false;
// }else {
// if (pathname.toString().endsWith(".jpg")){
// return true;
// }
// return false;
// }
// }
// });
File[] files1 = file.listFiles((dir, name)->{
return dir.toString().endsWith(".jpg") || name.endsWith(".jpg");
}
);
for (File file1 : files1) {
System.out.println(file1);
}
}
}