1 import java.io.File; 2 import java.io.IOException; 3
4 public class FileDemo { 5 public static void main(String[] args) { 6 // 抛除异常IOEcxception
7 try { 8 fun(); 9 } catch (IOException e) { 10 e.printStackTrace(); 11 } 12 } 13 public static void fun() throws IOException { 14 // 在d盘创建一个文件夹aaa madir()创建文件夹目录
15 File file = new File("d:\\aaa"); 16 boolean mkdir = file.mkdir(); 17 System.out.println(mkdir); 18 // 在d盘的文件夹aaa里面创建三个文本 createNewFile()创建文件
19 File file1 = new File("d:\\aaa\\aaa.txt"); 20 boolean newFile1 = file1.createNewFile(); 21 System.out.println(newFile1); 22 File file2 = new File("d:\\aaa\\bbb.txt"); 23 boolean newFile2 = file2.createNewFile(); 24 System.out.println(newFile2); 25 File file3 = new File("d:\\aaa\\ddd.txt"); 26 boolean newFile3 = file3.createNewFile(); 27 System.out.println(newFile3); 28
29 // 多创建了一个ccc.txt文本 30 // 删除文件夹内的ccc.txt文本
31 File file4 = new File("d:\\aaa\\ccc.txt"); 32 boolean delete = file4.delete(); 33 System.out.println(delete); 34
35 // 遍历文件夹aaa内的三个文本 list() 遍历
36 File file5 = new File("d:\\aaa"); 37 String[] list = file5.list(); 38 for(String i:list){ 39 System.out.println(i); 40 } 41 } 42 }
打印结果 再看看d盘有没有成功创建文件
