1.File类:文件和目录路径名的抽象表现形式
2.创建对象: File(String pathname) 通过给定的路径创建文件对象
File(String parent, String child) /File(File parent, String child)
3. 常用方法:
1 》 exists() 判断文件或目录是否在硬盘上存在。 true 表示存在。
代码示例:
1 public class File 类 { 2 public static void main(String[] args) { 3 File f=new File("d:\s\1.txt"); 4 File f1=new File("d:/1.txt"); 5 File f2=new File("a.txt"); 6 // System.out.println(f.exists()); 7 // System.out.println(f2.getAbsolutePath()); 8 } 9 }
文件的常用方法:
1 常用方法: 2 1.exists() 判断文件或目录是否在硬盘上存在。 true 表示存在 3 2.createNewFile() 创建一个文件,如果该文件已经存在,返回 false ,如果创建成功,返回 true 4 3.mkdirs() /mkdir() 创建文件夹,加 s 表示父目录可以不存在,不加,必须存在,才能创建成功 5 4.delete() /deleteOnExit() 删除文件或目录 /jvm 退出时删除 6 注意: 删除可以删除文件或者空目录。
代码示例:
1 public class File 类的常用方法 { 2 public static void main(String[] args) throws IOException { 3 File f=new File("d:\s\1.txt"); 4 File f1=new File("d:\s\2"); 5 File f2=new File("d:\s\a\3"); 6 // System.out.println(f.createNewFile()); 7 // System.out.println(f1.createNewFile()); 8 // System.out.println(f2.mkdirs()); 9 // System.out.println(f1.delete()); 10 System.out.println(f2.delete()); 11 } 12 }
文件的常用方法:
1 1.getAbsoluteFile() /getAbsolutePath() 获取文件或目录的绝对路径 2 2.getName() 返回文件或目录的名称,包括后缀名 3 3.getPath() 返回文件或目录的路径表示形式。 4 4.getParent() /getParentFile() 获取文件或目录的父目录 5 **5.isDirectory() /isFile() 判断是否为目录 / 文件 前提是该文件或目录必须存在,才能判断,否则都返回 fals 6 e 。 7 6.lastModified() 返回文件或目录的最后一次修改时间 返回值为毫秒值 long 类型 8 7.length() 返回文件的长度,如果文件不存在,返回值为 0 如果文件存在,返回值为文件的长度,如果为目录,值不 9 确定。
代码示例:
1 public class File 的常用方法一 { 2 public static void main(String[] args) { 3 File file=new File("1.txt"); 4 File file1=new File("d:\a\1.txt"); 5 /* System.out.println(file.getAbsolutePath()); 6 System.out.println(file.getName()); 7 System.out.println(file.getPath()); 8 System.out.println(file1.getName()); 9 System.out.println(file1.getPath());*/ 10 File f2=new File("d:\s\a.txt"); 11 // System.out.println(f2.isDirectory()); 12 // System.out.println(f2.isFile()); 13 // 最后一次修改时间 14 // System.out.println(new Date(f2.lastModified())); 15 System.out.println(f2.length()); 16 System.out.println(new File("d:\s").length()); 17 } 18 }
1 *** 1.list() /listFiles() 遍历文件夹 2 注意: 3 ① 只能遍历出该父目录下的直接子目录和子文件,不能再继续遍历,并且不包含该父目录。 4 ② 如果该父目录不是一个目录,或者不存在 都返回 null ,所以在遍历的时候,需要判断数组是否为 null 5 再进行遍历。 6 代码演示 7 public class File 类的常用方法二 { 8 public static void main(String[] args) { 9 File file=new File("d:\s"); 10 File[] arr = file.listFiles(); 11 if (arr!=null) { 12 for (File file2 : arr) { 13 System.out.println(file2); 14 } 15 } 16 } 17 }
递归:自己调用自己
1 注意 : 2 ① 递归必须有出口。没有出口会导致栈溢出 3 ② 递归的分类: 4 1 》 直接递归 5 例如: A 自身在 A 方法中被调用。 6 2 》间接递归 7 例如: A 在 B 方法中使用, B 方法又在 C 方法中使用。。 C 方法中又调用了 A 方法。 8 2. 用递归 ,加法 求 1-10 的和 9 代码演示: 10 public class 递归 { 11 public static void main(String[] args) { 12 System.out.println(getSum(5)); 13 } 14 // 用递归 ,加法 求 1-10 的和 15 public static int getSum(int n) { 16 // 当 n=1 和就是 1 17 if(n==1) { 18 return 1; 19 } 20 return getSum(n-1)+n; 21 } 22 }
递归中的兔子问题(斐波那契数列)
1 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少 2 3 n=3 n=(n-1)+(n-2) 4 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
利用递归:
代码示例:
1 package com.niubi; 2 3 public class Demo12 { 4 5 public static void main(String[] args) { 6 7 System.out.println("第十二个月共有"+getRabbit(12)+"只兔子"); 8 //假设求第十二个月总共有多少对兔子 9 } 10 public static int getRabbit(int n) { 11 if(n==1||n==2) { 12 return 1; 13 } 14 else { 15 return getRabbit(n-1)+getRabbit(n-2); 16 } 17 } 18 19 }
执行结果: