1:集合(本身补齐) Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程安全,效率低 LinkedList 底层数据结构是链表,查询慢,增删快 线程不安全,效率高 Set(无序,惟一) HashSet 底层数据结构是哈希表。 哈希表依赖两个方法:hashCode()和equals() 执行顺序: 首先判断hashCode()值是否相同 是:继续执行equals(),看其返回值 是true:说明元素重复,不添加 是false:就直接添加到集合 否:就直接添加到集合 最终: 自动生成hashCode()和equals()便可 LinkedHashSet 底层数据结构由链表和哈希表组成。 由链表保证元素有序。 由哈希表保证元素惟一。 TreeSet 底层数据结构是红黑树。(是一种自平衡的二叉树) 如何保证元素惟一性呢? 根据比较的返回值是不是0来决定 如何保证元素的排序呢? 两种方式 天然排序(元素具有比较性) 让元素所属的类实现Comparable接口 比较器排序(集合具有比较性) 让集合接收一个Comparator的实现类对象 Map(双列集合) A:Map集合的数据结构仅仅针对键有效,与值无关。 B:存储的是键值对形式的元素,键惟一,值可重复。 HashMap 底层数据结构是哈希表。线程不安全,效率高 哈希表依赖两个方法:hashCode()和equals() 执行顺序: 首先判断hashCode()值是否相同 是:继续执行equals(),看其返回值 是true:说明元素重复,不添加 是false:就直接添加到集合 否:就直接添加到集合 最终: 自动生成hashCode()和equals()便可 LinkedHashMap 底层数据结构由链表和哈希表组成。 由链表保证元素有序。 由哈希表保证元素惟一。 Hashtable 底层数据结构是哈希表。线程安全,效率低 哈希表依赖两个方法:hashCode()和equals() 执行顺序: 首先判断hashCode()值是否相同 是:继续执行equals(),看其返回值 是true:说明元素重复,不添加 是false:就直接添加到集合 否:就直接添加到集合 最终: 自动生成hashCode()和equals()便可 TreeMap 底层数据结构是红黑树。(是一种自平衡的二叉树) 如何保证元素惟一性呢? 根据比较的返回值是不是0来决定 如何保证元素的排序呢? 两种方式 天然排序(元素具有比较性) 让元素所属的类实现Comparable接口 比较器排序(集合具有比较性) 让集合接收一个Comparator的实现类对象 2:到底使用那种集合(本身补齐) 看需求。 是不是键值对象形式: 是:Map 键是否须要排序: 是:TreeMap 否:HashMap 不知道,就使用HashMap。 否:Collection 元素是否惟一: 是:Set 元素是否须要排序: 是:TreeSet 否:HashSet 不知道,就使用HashSet 否:List 要安全吗: 是:Vector(其实咱们也不用它,后面咱们讲解了多线程之后,我在给你回顾用谁) 否:ArrayList或者LinkedList 增删多:LinkedList 查询多:ArrayList 不知道,就使用ArrayList 不知道,就使用ArrayList 3:集合的常见方法及遍历方式 Collection: add() remove() contains() iterator() size() 遍历: 加强for 迭代器 |--List get() 遍历: 普通for |--Set Map: put() remove() containskey(),containsValue() keySet() get() value() entrySet() size() 遍历: 根据键找值 根据键值对对象分别找键和值 做业: 我讲解过的任意一个集合,我要求你存储什么,你就可以存储什么。 而且,还要可以遍历出来。 4:ArrayList,LinkedList,HashSet,HashMap(掌握) 存储字符串和自定义对象数据并遍历 5:集合的嵌套遍历(理解)
异常处理
html
package cn.itcast_02; /* * A:一个异常 * B:二个异常的处理 * a:每个写一个try...catch * b:写一个try,多个catch * try{ * ... * }catch(异常类名 变量名) { * ... * } * catch(异常类名 变量名) { * ... * } * ... * * 注意事项: * 1:能明确的尽可能明确,不要用大的来处理。 * 2:平级关系的异常谁前谁后无所谓,若是出现了子父关系,父必须在后面。 * * 注意: * 一旦try里面出了问题,就会在这里把问题给抛出去,而后和catch里面的问题进行匹配, * 一旦有匹配的,就执行catch里面的处理,而后结束了try...catch * 继续执行后面的语句。 */ public class ExceptionDemo2 { public static void main(String[] args) { // method1(); // method2(); // method3(); method4(); } public static void method4() { int a = 10; int b = 0; int[] arr = { 1, 2, 3 }; // 爷爷在最后 try { System.out.println(a / b); System.out.println(arr[3]); System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?"); } catch (ArithmeticException e) { System.out.println("除数不能为0"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("你访问了不应的访问的索引"); } catch (Exception e) { System.out.println("出问题了"); } // 爷爷在前面是不能够的 // try { // System.out.println(a / b); // System.out.println(arr[3]); // System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?"); // } catch (Exception e) { // System.out.println("出问题了"); // } catch (ArithmeticException e) { // System.out.println("除数不能为0"); // } catch (ArrayIndexOutOfBoundsException e) { // System.out.println("你访问了不应的访问的索引"); // } System.out.println("over"); } // 两个异常的处理 public static void method3() { int a = 10; int b = 0; int[] arr = { 1, 2, 3 }; try { System.out.println(arr[3]); System.out.println(a / b); // System.out.println(arr[3]); } catch (ArithmeticException e) { System.out.println("除数不能为0"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("你访问了不应的访问的索引"); } System.out.println("over"); } // 两个异常 public static void method2() { int a = 10; int b = 0; try { System.out.println(a / b); } catch (ArithmeticException e) { System.out.println("除数不能为0"); } int[] arr = { 1, 2, 3 }; try { System.out.println(arr[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("你访问了不应的访问的索引"); } System.out.println("over"); } // 一个异常 public static void method1() { // 第一阶段 int a = 10; // int b = 2; int b = 0; try { System.out.println(a / b); } catch (ArithmeticException ae) { System.out.println("除数不能为0"); } // 第二阶段 System.out.println("over"); } }
package cn.itcast_02; /* * JDK7出现了一个新的异常处理方案: * try{ * * }catch(异常名1 | 异常名2 | ... 变量 ) { * ... * } * * 注意:这个方法虽然简洁,可是也不够好。 * A:处理方式是一致的。(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理) * B:多个异常间必须是平级关系。 */ public class ExceptionDemo3 { public static void main(String[] args) { method(); } public static void method() { int a = 10; int b = 0; int[] arr = { 1, 2, 3 }; // try { // System.out.println(a / b); // System.out.println(arr[3]); // System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?"); // } catch (ArithmeticException e) { // System.out.println("除数不能为0"); // } catch (ArrayIndexOutOfBoundsException e) { // System.out.println("你访问了不应的访问的索引"); // } catch (Exception e) { // System.out.println("出问题了"); // } // JDK7的处理方案 try { System.out.println(a / b); System.out.println(arr[3]); } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("出问题了"); } System.out.println("over"); } }
package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /* * 在try里面发现问题后,jvm会帮咱们生成一个异常对象,而后把这个对象抛出,和catch里面的类进行匹配。 * 若是该对象是某个类型的,就会执行该catch里面的处理信息。 * * 异常中要了解的几个方法: * public String getMessage():异常的消息字符串 * public String toString():返回异常的简单信息描述 * 此对象的类的 name(全路径名) * ": "(冒号和一个空格) * 调用此对象 getLocalizedMessage()方法的结果 (默认返回的是getMessage()的内容) * printStackTrace() 获取异常类名和异常信息,以及异常出如今程序中的位置。返回值void。把信息输出在控制台。 */ public class ExceptionDemo { public static void main(String[] args) { String s = "2014-11-20"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date d = sdf.parse(s); // 建立了一个ParseException对象,而后抛出去,和catch里面进行匹配 System.out.println(d); } catch (ParseException e) { // ParseException e = new ParseException(); // ParseException // e.printStackTrace(); // getMessage() // System.out.println(e.getMessage()); // Unparseable date: "2014-11-20" // toString() // System.out.println(e.toString()); // java.text.ParseException: Unparseable date: "2014-11-20" e.printStackTrace(); //跳转到某个指定的页面(index.html) } System.out.println("over"); } }
throws的方式处理异常
java
package cn.itcast_05; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /* * 有些时候,咱们是能够对异常进行处理的,可是又有些时候,咱们根本就没有权限去处理某个异常。 * 或者说,我处理不了,我就不处理了。 * 为了解决出错问题,Java针对这种状况,就提供了另外一种处理方案:抛出。 * * 格式: * throws 异常类名 * 注意:这个格式必须跟在方法的括号后面。 * * 注意: * 尽可能不要在main方法上抛出异常。 * 可是我讲课为了方便我就这样作了。 * * 小结: * 编译期异常抛出,未来调用者必须处理。 * 运行期异常抛出,未来调用能够不用处理。 */ public class ExceptionDemo { public static void main(String[] args) { System.out.println("今每天气很好"); try { method(); } catch (ParseException e) { e.printStackTrace(); } System.out.println("可是就是不应有雾霾"); method2(); } // 运行期异常的抛出 public static void method2() throws ArithmeticException { int a = 10; int b = 0; System.out.println(a / b); } // 编译期异常的抛出 // 在方法声明上抛出,是为了告诉调用者,你注意了,我有问题。 public static void method() throws ParseException { String s = "2014-11-20"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = sdf.parse(s); System.out.println(d); } }
package cn.itcast_06; /* * throw:若是出现了异常状况,咱们能够把该异常抛出,这个时候的抛出的应该是异常的对象。 * * throws和throw的区别(面试题) throws 用在方法声明后面,跟的是异常类名 能够跟多个异常类名,用逗号隔开 表示抛出异常,由该方法的调用者来处理 throws表示出现异常的一种可能性,并不必定会发生这些异常 throw 用在方法体内,跟的是异常对象名 只能抛出一个异常对象名 表示抛出异常,由方法体内的语句处理 throw则是抛出了异常,执行throw则必定抛出了某种异常 */ public class ExceptionDemo { public static void main(String[] args) { // method(); try { method2(); } catch (Exception e) { e.printStackTrace(); } } public static void method() { int a = 10; int b = 0; if (b == 0) { throw new ArithmeticException(); } else { System.out.println(a / b); } } public static void method2() throws Exception { int a = 10; int b = 0; if (b == 0) { throw new Exception(); } else { System.out.println(a / b); } } }
/* * finally:被finally控制的语句体必定会执行 * 注意:若是在执行到finally以前jvm退出了,就不能执行了。 * * A:格式 * try...catch...finally... * B:用于释放资源,在IO流操做和数据库操做中会见到 */
package cn.itcast_07; /* * 面试题: * 1:final,finally和finalize的区别 * final:最终的意思,能够修饰类,成员变量,成员方法 * 修饰类,类不能被继承 * 修饰变量,变量是常量 * 修饰方法,方法不能被重写 * finally:是异常处理的一部分,用于释放资源。 * 通常来讲,代码确定会执行,特殊状况:在执行到finally以前jvm退出了 * finalize:是Object类的一个方法,用于垃圾回收 * * 2:若是catch里面有return语句,请问finally里面的代码还会执行吗? * 若是会,请问是在return前,仍是return后。 * 会。前。 * * 准确的说,应该是在中间。 * * 3:try...catch...finally的格式变形 * A:try...catch...finally * B:try...catch * C:try...catch...catch... * D:try...catch...catch...finally * E:try...finally * 这种作法的目前是为了释放资源。 */ public class FinallyDemo2 { public static void main(String[] args) { System.out.println(getInt()); } public static int getInt() { int a = 10; try { System.out.println(a / 0); a = 20; } catch (ArithmeticException e) { a = 30; return a; /* * return a在程序执行到这一步的时候,这里不是return a而是return 30;这个返回路径就造成了。 * 可是呢,它发现后面还有finally,因此继续执行finally的内容,a=40 * 再次回到之前的返回路径,继续走return 30; */ } finally { a = 40; return a;//若是这样结果就是40了。 } // return a; } }
自定义异常的实现和测试
面试
package cn.itcast_08; /* * java不可能对全部的状况都考虑到,因此,在实际的开发中,咱们可能须要本身定义异常。 * 而咱们本身随意的写一个类,是不能做为异常类来看的,要想你的类是一个异常类,就必须继承自Exception或者RuntimeException * * 两种方式: * A:继承Exception * B:继承RuntimeException */ public class MyException extends Exception { public MyException() { } public MyException(String message) { super(message); } } // public class MyException extends RuntimeException { // // }
package cn.itcast_08; public class Teacher { public void check(int score) throws MyException { if (score > 100 || score < 0) { throw new MyException("分数必须在0-100之间"); } else { System.out.println("分数没有问题"); } } // 针对MyException继承自RuntimeException // public void check(int score) { // if (score > 100 || score < 0) { // throw new MyException(); // } else { // System.out.println("分数没有问题"); // } // } }
package cn.itcast_08; import java.util.Scanner; /* * 自定义异常测试类 */ public class StudentDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生成绩:"); int score = sc.nextInt(); Teacher t = new Teacher(); try { t.check(score); } catch (MyException e) { e.printStackTrace(); } } }
异常的注意事项
数据库
package cn.itcast_09; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /* * 异常注意事项: * A:子类重写父类方法时,子类的方法必须抛出相同的异常或父类异常的子类。(父亲坏了,儿子不能比父亲更坏) * B:若是父类抛出了多个异常,子类重写父类时,只能抛出相同的异常或者是他的子集,子类不能抛出父类没有的异常 * C:若是被重写的方法没有异常抛出,那么子类的方法绝对不能够抛出异常,若是子类方法内有异常发生,那么子类只能try,不能throws */ public class ExceptionDemo { } class Fu { public void show() throws Exception { } public void method() { } } class Zi extends Fu { @Override public void show() throws ArithmeticException { } @Override public void method() { // String s = "2014-11-20"; // SimpleDateFormat sdf = new SimpleDateFormat(); // Date d = sdf.parse(s); // System.out.println(d); } }
File类的建立功能
数组
package cn.itcast_02; import java.io.File; import java.io.IOException; /* *建立功能: *public boolean createNewFile():建立文件 若是存在这样的文件,就不建立了 *public boolean mkdir():建立文件夹 若是存在这样的文件夹,就不建立了 *public boolean mkdirs():建立文件夹,若是父文件夹不存在,会帮你建立出来 * *骑白马的不必定是王子,多是班长。 *注意:你到底要建立文件仍是文件夹,你最清楚,方法不要调错了。 */ public class FileDemo { public static void main(String[] args) throws IOException { // 需求:我要在e盘目录下建立一个文件夹demo File file = new File("e:\\demo"); System.out.println("mkdir:" + file.mkdir()); // 需求:我要在e盘目录demo下建立一个文件a.txt File file2 = new File("e:\\demo\\a.txt"); System.out.println("createNewFile:" + file2.createNewFile()); // 需求:我要在e盘目录test下建立一个文件b.txt // Exception in thread "main" java.io.IOException: 系统找不到指定的路径。 // 注意:要想在某个目录下建立内容,该目录首先必须存在。 // File file3 = new File("e:\\test\\b.txt"); // System.out.println("createNewFile:" + file3.createNewFile()); // 需求:我要在e盘目录test下建立aaa目录 // File file4 = new File("e:\\test\\aaa"); // System.out.println("mkdir:" + file4.mkdir()); // File file5 = new File("e:\\test"); // File file6 = new File("e:\\test\\aaa"); // System.out.println("mkdir:" + file5.mkdir()); // System.out.println("mkdir:" + file6.mkdir()); // 其实咱们有更简单的方法 File file7 = new File("e:\\aaa\\bbb\\ccc\\ddd"); System.out.println("mkdirs:" + file7.mkdirs()); // 看下面的这个东西: File file8 = new File("e:\\liuyi\\a.txt"); System.out.println("mkdirs:" + file8.mkdirs()); } }
File类的删除功能
安全
package cn.itcast_03; import java.io.File; import java.io.IOException; /* * 删除功能:public boolean delete() * * 注意: * A:若是你建立文件或者文件夹忘了写盘符路径,那么,默认在项目路径下。 * B:Java中的删除不走回收站。 * C:要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹 */ public class FileDemo { public static void main(String[] args) throws IOException { // 建立文件 // File file = new File("e:\\a.txt"); // System.out.println("createNewFile:" + file.createNewFile()); // 我不当心写成这个样子了 File file = new File("a.txt"); System.out.println("createNewFile:" + file.createNewFile()); // 继续玩几个 File file2 = new File("aaa\\bbb\\ccc"); System.out.println("mkdirs:" + file2.mkdirs()); // 删除功能:我要删除a.txt这个文件 File file3 = new File("a.txt"); System.out.println("delete:" + file3.delete()); // 删除功能:我要删除ccc这个文件夹 File file4 = new File("aaa\\bbb\\ccc"); System.out.println("delete:" + file4.delete()); // 删除功能:我要删除aaa文件夹 // File file5 = new File("aaa"); // System.out.println("delete:" + file5.delete()); File file6 = new File("aaa\\bbb"); File file7 = new File("aaa"); System.out.println("delete:" + file6.delete()); System.out.println("delete:" + file7.delete()); } }
File类的重命名功能
数据结构
package cn.itcast_04; import java.io.File; /* * 重命名功能:public boolean renameTo(File dest) * 若是路径名相同,就是更名。 * 若是路径名不一样,就是更名并剪切。 * * 路径以盘符开始:绝对路径 c:\\a.txt * 路径不以盘符开始:相对路径 a.txt */ public class FileDemo { public static void main(String[] args) { // 建立一个文件对象 // File file = new File("林青霞.jpg"); // // 需求:我要修改这个文件的名称为"东方不败.jpg" // File newFile = new File("东方不败.jpg"); // System.out.println("renameTo:" + file.renameTo(newFile)); File file2 = new File("东方不败.jpg"); File newFile2 = new File("e:\\林青霞.jpg"); System.out.println("renameTo:" + file2.renameTo(newFile2)); } }
File类的判断功能
多线程
package cn.itcast_05; import java.io.File; /* * 判断功能: * public boolean isDirectory():判断是不是目录 * public boolean isFile():判断是不是文件 * public boolean exists():判断是否存在 * public boolean canRead():判断是否可读 * public boolean canWrite():判断是否可写 * public boolean isHidden():判断是否隐藏 */ public class FileDemo { public static void main(String[] args) { // 建立文件对象 File file = new File("a.txt"); System.out.println("isDirectory:" + file.isDirectory());// false System.out.println("isFile:" + file.isFile());// true System.out.println("exists:" + file.exists());// true System.out.println("canRead:" + file.canRead());// true System.out.println("canWrite:" + file.canWrite());// true System.out.println("isHidden:" + file.isHidden());// false } }
File类的获取功能
jvm
package cn.itcast_06; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /* * 获取功能: * public String getAbsolutePath():获取绝对路径 * public String getPath():获取相对路径 * public String getName():获取名称 * public long length():获取长度。字节数 * public long lastModified():获取最后一次的修改时间,毫秒值 */ public class FileDemo { public static void main(String[] args) { // 建立文件对象 File file = new File("demo\\test.txt"); System.out.println("getAbsolutePath:" + file.getAbsolutePath()); System.out.println("getPath:" + file.getPath()); System.out.println("getName:" + file.getName()); System.out.println("length:" + file.length()); System.out.println("lastModified:" + file.lastModified()); // 1416471971031 Date d = new Date(1416471971031L); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = sdf.format(d); System.out.println(s); } }
File类的高级获取功能
ide
package cn.itcast_07; import java.io.File; /* * 获取功能: * public String[] list():获取指定目录下的全部文件或者文件夹的名称数组 * public File[] listFiles():获取指定目录下的全部文件或者文件夹的File数组 */ public class FileDemo { public static void main(String[] args) { // 指定一个目录 File file = new File("e:\\"); // public String[] list():获取指定目录下的全部文件或者文件夹的名称数组 String[] strArray = file.list(); for (String s : strArray) { System.out.println(s); } System.out.println("------------"); // public File[] listFiles():获取指定目录下的全部文件或者文件夹的File数组 File[] fileArray = file.listFiles(); for (File f : fileArray) { System.out.println(f.getName()); } } }
输出指定目录下指定后缀名的文件名称案例
package cn.itcast_08; import java.io.File; /* * 判断E盘目录下是否有后缀名为.jpg的文件,若是有,就输出此文件名称 * * 分析: * A:封装e判断目录 * B:获取该目录下全部文件或者文件夹的File数组 * C:遍历该File数组,获得每个File对象,而后判断 * D:是不是文件 * 是:继续判断是否以.jpg结尾 * 是:就输出该文件名称 * 否:不搭理它 * 否:不搭理它 */ public class FileDemo { public static void main(String[] args) { // 封装e判断目录 File file = new File("e:\\"); // 获取该目录下全部文件或者文件夹的File数组 File[] fileArray = file.listFiles(); // 遍历该File数组,获得每个File对象,而后判断 for (File f : fileArray) { // 是不是文件 if (f.isFile()) { // 继续判断是否以.jpg结尾 if (f.getName().endsWith(".jpg")) { // 就输出该文件名称 System.out.println(f.getName()); } } } } }
利用过滤器改进
package cn.itcast_08; import java.io.File; import java.io.FilenameFilter; /* * 判断E盘目录下是否有后缀名为.jpg的文件,若是有,就输出此文件名称 * A:先获取全部的,而后遍历的时候,依次判断,若是知足条件就输出。 * B:获取的时候就已是知足条件的了,而后输出便可。 * * 要想实现这个效果,就必须学习一个接口:文件名称过滤器 * public String[] list(FilenameFilter filter) * public File[] listFiles(FilenameFilter filter) */ public class FileDemo2 { public static void main(String[] args) { // 封装e判断目录 File file = new File("e:\\"); // 获取该目录下全部文件或者文件夹的String数组 // public String[] list(FilenameFilter filter) String[] strArray = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // return false; // return true; // 经过这个测试,咱们就知道了,到底把这个文件或者文件夹的名称加不加到数组中,取决于这里的返回值是true仍是false // 因此,这个的true或者false应该是咱们经过某种判断获得的 // System.out.println(dir + "---" + name); // File file = new File(dir, name); // // System.out.println(file); // boolean flag = file.isFile(); // boolean flag2 = name.endsWith(".jpg"); // return flag && flag2; return new File(dir, name).isFile() && name.endsWith(".jpg"); } }); // 遍历 for (String s : strArray) { System.out.println(s); } } }
批量修改文件名称案例
package cn.itcast_09; import java.io.File; /* * 需求:把E:\评书\三国演义下面的视频名称修改成 * 00?_介绍.avi * * 思路: * A:封装目录 * B:获取该目录下全部的文件的File数组 * C:遍历该File数组,获得每个File对象 * D:拼接一个新的名称,而后重命名便可。 */ public class FileDemo { public static void main(String[] args) { // 封装目录 File srcFolder = new File("E:\\评书\\三国演义"); // 获取该目录下全部的文件的File数组 File[] fileArray = srcFolder.listFiles(); // 遍历该File数组,获得每个File对象 for (File file : fileArray) { // System.out.println(file); // E:\评书\三国演义\三国演义_001_[评书网-今天很高兴,明天就IO了]_桃园三结义.avi // 改后:E:\评书\三国演义\001_桃园三结义.avi String name = file.getName(); // 三国演义_001_[评书网-今天很高兴,明天就IO了]_桃园三结义.avi int index = name.indexOf("_"); String numberString = name.substring(index + 1, index + 4); // System.out.println(numberString); // int startIndex = name.lastIndexOf('_'); // int endIndex = name.lastIndexOf('.'); // String nameString = name.substring(startIndex + 1, endIndex); // System.out.println(nameString); int endIndex = name.lastIndexOf('_'); String nameString = name.substring(endIndex); String newName = numberString.concat(nameString); // 001_桃园三结义.avi // System.out.println(newName); File newFile = new File(srcFolder, newName); // E:\\评书\\三国演义\\001_桃园三结义.avi // 重命名便可 file.renameTo(newFile); } } }
总结
1:异常(理解) (1)程序出现的不正常的状况。 (2)异常的体系 Throwable |--Error 严重问题,咱们不处理。 |--Exception |--RuntimeException 运行期异常,咱们须要修正代码 |--非RuntimeException 编译期异常,必须处理的,不然程序编译不经过 (3)异常的处理: A:JVM的默认处理 把异常的名称,缘由,位置等信息输出在控制台,可是呢程序不能继续执行了。 B:本身处理 a:try...catch...finally 本身编写处理代码,后面的程序能够继续执行 b:throws 把本身处理不了的,在方法上声明,告诉调用者,这里有问题 (4)面试题 A:编译期异常和运行期异常的区别? 编译期异常 必需要处理的,不然编译不经过 运行期异常 能够不处理,也能够处理 B:throw和throws是的区别 throw: 在方法体中,后面跟的是异常对象名,而且只能是一个 throw抛出的是一个异常对象,说明这里确定有一个异常产生了 throws: 在方法声明上,后面跟的是异常的类名,能够是多个 throws是声明方法有异常,是一种可能性,这个异常并不必定会产生 (5)finally关键字及其面试题 A:finally用于释放资源,它的代码永远会执行。特殊状况:在执行到finally以前jvm退出了 B:面试题 a:final,finally,finalize的区别? b:若是在catch里面有return,请问finally还执行吗?若是执行,在return前仍是后 会,前。 实际上在中间。这个上课咱们讲过 C:异常处理的变形 try...catch...finally try...catch... try...catch...catch... try...catch...catch...fianlly try...finally (6)自定义异常 继承自Exception或者RuntimeException,只须要提供无参构造和一个带参构造便可 (7)异常的注意实现 A:父的方法有异常抛出,子的重写方法在抛出异常的时候必需要小于等于父的异常 B:父的方法没有异常抛出,子的重写方法不能有异常抛出 C:父的方法抛出多个异常,子的重写方法必须比父少或者小 2:File(掌握) (1)IO流操做中大部分都是对文件的操做,因此Java就提供了File类供咱们来操做文件 (2)构造方法 A:File file = new File("e:\\demo\\a.txt"); B:File file = new File("e:\\demo","a.txt"); C:File file = new File("e:\\demo"); File file2 = new File(file,"a.txt"); (3)File类的功能(本身补齐) A:建立功能 B:删除功能 C:重命名功能 D:判断功能 E:获取功能 F:高级获取功能 G:过滤器功能 (4)案例: A:输出指定目录下指定后缀名的文件名称 a:先获取全部的,在遍历的时候判断,再输出 b:先判断,再获取,最后直接遍历输出便可 B:批量修改文件名称