作业:遍历所有对象
f:/yifan/111.txt
/aa.txt
/aa/aaa.txt
找出所有文件
和路径
显示结果要求:
f:/yifan/111.txt
/aa.txt
/aa/aaa.txt
package day3;
import java.io.File;
import java.io.IOException;
public class Task5 {
public static void main(String[] args) {
File file = new File("F:/yifan");
boolean yu= file.mkdir();
System.out.println(yu);
File file1= new File(new File("F:/yifan"),"aa");
boolean shu= file1.mkdir();
System.out.println(shu);
File file2 = new File("F:/yifan/aa.txt");
File file3 = new File("F:/yifan/111.txt");
File file4 = new File("F:/yifan/aa/aaa.txt");
try {
file2.createNewFile();
file3.createNewFile();
file4.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
String[] fileArr = file.list();
for(int i =0; i< fileArr.length; i++){
System.out.println(fileArr[i]);
}
String fileName = file.getName();
System.out.println(fileName);
System.out.println(file.getAbsolutePath());
String[] fileArr1 = file1.list();
for(int j =0; j< fileArr1.length; j++){
System.out.println(fileArr1[j]);
}
String fileName1 = file1.getName();
System.out.println(fileName1);
System.out.println(file1.getAbsolutePath());
}
}