-----------------------------------day19 总结------------------------------------------
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:批量修改文件名称
------------------------------------------知识点1--------------------------------------------------
package cn.itcast_01;
/*
-
异常:程序出现了不正常的情况
-
举例:
-
程序异常:Throwable:Throwable 类是 Java 语言中所有错误或异常的超类
-
严重问题:Error 我们不处理,这种问题一般都是很严重的问题,比如说内存溢出。
-
问题:Exception
-
编译问题:不是RunTimeException的异常 必须进行处理,因为你不处理,编译通不过。
-
运行问题:RunTimeException 这种问题我们也不处理,因为代码不够严谨,需要修正代码。
-
*/
public class ExceptionDemo {
public static void main(String[] args) {
int a =10;
int b = 0;
// 第一阶段程序
System.out.println(a/b);// 第二阶段程序 System.out.println("over");
}
}
package cn.itcast_02;
/* -
我们如何处理异常?
-
1.try…catch…finally
-
2.throws 抛出
-
try…catch…finally的处理格式:
-
try{
-
可能出现问题的代码;
-
}catch(异常名 变量){
-
针对问题的处理;
-
}finally{
-
释放资源;
-
}
-
变形格式:
-
try{
-
可能出现问题的代码;
-
}catch(异常名 变量){
-
针对问题的处理;
-
}
-
注意:
-
1.try里面的代码越少越好
-
2.catch里面必须有内容,哪怕是给出一个简单的提示
-
*/
public class ExceptionDemo {
public static void main(String[] args) {
int a =10;
int b = 0;
try{
System.out.println(a/b);
}catch(ArithmeticException ae){
System.out.println(“除数不能为0”);
}
// 第一阶段程序
// System.out.println(a/b);// 第二阶段程序 System.out.println("over");
}
}
------------------------------------------知识点2--------------------------------------------------
package cn.itcast_02;
/* -
1.一个异常
-
2.两个异常
-
a:每一个写一个try...catch
-
b:写一个try,多个catch
-
try{
-
...
-
}catch(异常类名 变量名){
-
...
-
}
-
...
-
注意事项:
-
1.能明确的尽量明确,不要用大的来处理
-
2.平级关系的异常谁先谁后无所谓,如果出现了子父关系,父必须在后面。
-
注意:
-
一旦try里面出了问题,就会在这里把问题抛出去,然后和后面的catch匹配,
-
一旦有了匹配的,就执行catch里面的处理,然后结束了try...catch
-
继续执行后面的语句。
-
*/
public class ExceptionDemo2 {
public static void main(String[] args) {
method3();}
// 爷爷类放在最后,标准写法。
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]); }catch(ArithmeticException ae){ System.out.println("除数不能为0"); }catch(ArrayIndexOutOfBoundsException aoobse){ System.out.println("数组的索引越界"); }catch(Exception 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(a/b); System.out.println(arr[3]); }catch(ArithmeticException ae){ System.out.println("除数不能为0"); }catch(ArrayIndexOutOfBoundsException aoobse){ 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 ae){
System.out.println(“除数不能为0”);
}
int [] arr ={1,2,3};
try{
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException aoobse){
System.out.println(“数组的索引越界”);
}
System.out.println(“over”);
}// 一个异常
public static void method1(){
int a =10;
int b = 0;
// 第一阶段程序
System.out.println(a/b);// 第二阶段程序 System.out.println("over");
}
}
------------------------------------------知识点3--------------------------------------------------
package cn.itcast_02;
/* -
JDK7出现了一个新的异常处理方案:
-
try{
-
}catch(异常1|异常2|...变量){
-
...
-
}
-
注意:这个方法虽然简洁,但是不够好
-
1.处理方式一致的。(实际开发中,符合要求)
-
2.异常必须是同级关系
-
*/
public class ExceptionDemo3 {
public static void main(String[] args) {
method2();}
// 爷爷类放在最后,标准写法。
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]); }catch(ArithmeticException ae){ System.out.println("除数不能为0"); }catch(ArrayIndexOutOfBoundsException aoobse){ System.out.println("数组的索引越界"); }catch(Exception e){ System.out.println("出错了"); } System.out.println("over");
}
// JDK7的处理方法
public static void method2(){
int a = 10;
int b = 0;
int [] arr ={1,2,3};try{ System.out.println(a/b); System.out.println(arr[3]); }catch(ArithmeticException|ArrayIndexOutOfBoundsException e){ System.out.println("出错了"); }
}
}
------------------------------------------知识点4--------------------------------------------------
package cn.itcast_03;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
-
编译时异常和运行时的区别
-
编译期异常:Java程序必须显示处理,否则程序就会发生错误,无法通过编译
-
运行期异常:无需显示处理,也可以和编译时异常一样处理
-
*/
public class ExceptionDemo {
public static void main(String[] args) {
String s = “2014-11-20”;
SimpleDateFormat sdf =new SimpleDateFormat(“yyyy-MM-dd”);
try {
Date d = sdf.parse(s);
System.out.println(d);
} catch (ParseException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println(“解析日期出现异常”);
}}
}
------------------------------------------知识点5--------------------------------------------------
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()的内容)
3.printStackTrace()
获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。把信息输出在控制台。
- public String getMessage()
-
*/
public class EceptionDemo {
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
System.out.println(d);
} catch (ParseException e) { // ParseException e = new 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" // printStackTrace() e.printStackTrace(); // 跳转到指定页面 } System.out.println("over");
}
}
------------------------------------------知识点6--------------------------------------------------
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) {
// TODO Auto-generated catch block
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 = “2019-08-26”;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-mm-dd HH:mm:ss”);
Date d = sdf.parse(s);
System.out.println(d);
}
}
------------------------------------------知识点7--------------------------------------------------
package cn.itcast_06;
/*
-
throw:如果出现了异常情况,我们就可以把该异常抛出,这个时候抛出的应该是异常对象
-
throws和throw的区别(面试题):
-
throws
用在方法声明后面,跟的是异常类名
可以跟多个异常类名,用逗号隔开
表示抛出异常,由该方法的调用者来处理
throws表示出现异常的一种可能性,并不一定会发生这些异常
throw
用在方法体内,跟的是异常对象名
只能抛出一个异常对象名
表示抛出异常,由方法体内的语句处理
throw则是抛出了异常,执行throw则一定抛出了某种异常 -
*/
public class ExceptionDemo {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}public static void method2() {
int a =10;
int b =0;
if(b==0){
throw new ArithmeticException();
}else{
System.out.println(a/b);
}
}public static void method() throws Exception {
int a =10;
int b =0;
if(b==0){
throw new Exception();
}else{
System.out.println(a/b);
}
}
}
------------------------------------------知识点8--------------------------------------------------
package cn.itcast_07;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
- finally:被finally控制的语句体一定会执行
- 注意:如果在执行到finally之前jvm推出了,就不能执行。
- A.格式:try…catch…finally
- B.用于释放资源,在IO流操作和数据库操作中会见到
**/
public class FinallyDemo {
public static void main(String[] args) {
method();
}
public static void method() {
String s = "2019-08-26";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date d =null;
try{
d = sdf.parse(s);
}catch(ParseException e){
e.printStackTrace();
}finally{
System.out.println("这里的代码一定会被执行");
}
System.out.println(d);
}
}
------------------------------------------知识点9--------------------------------------------------
package cn.itcast_07;
/*
-
1.final,finally和finalize的区别
-
final:最终的意思,可以修饰类,成员变量,成员方法、
-
修饰类,类不能被继承
-
修饰变量,变量是常量
-
修饰方法,方法不能被重写
-
finally:是异常处理的一部分,用于释放资源
-
finalize:是Object类的一个方法,用于垃圾回收,很少见
-
2:如果catch里面有return语句,请问finally里面的代码还会执行
-
如果会,请问是在return前还是return后。
-
会,前。
-
3.try…catch…finally的格式变形
-
1.try...catch...finally
-
2.try...catch
-
3.try...catch...catch..
-
4.try...catch...catch...finally
-
5.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 30;这个返回路径就形成了
* 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
* 再次回到以前的路径,继续return 30;
*
* /
}finally{
a=40;
//return a;
}
return a;
}
}
------------------------------------------知识点10--------------------------------------------------
package cn.itcast_08;
/ -
java不可能对所有情况都考虑到,所以实际开发中,需要我们自定义异常
-
而我们自己随意写的一个类,是不能作为异常类来看的,要想你的类是一个异常类,
-
就必须继承自Exception或者RuntimeException
-
两种方式:
-
1.继承Exception
-
2.继承RuntimeException
-
*/
public class MyException extends Exception{
public MyException(){}
public MyException(String s){
super(s);
}
}
------------------------------------------知识点11--------------------------------------------------
package cn.itcast_08;
import java.util.Scanner;
/*
-
自定义测试类
-
*/
public class Student {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入学生成绩:”);
int score = sc.nextInt();
try {
Teacher.check(score);
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
------------------------------------------知识点12--------------------------------------------------
package cn.itcast_08;
public class Teacher {
public static void check(int score) throws MyException{
if(score>100||score<0){
throw new MyException(“分数必须在0-100之间”);
}else{
System.out.println(“分数没问题!”);
}
}
}
------------------------------------------知识点13--------------------------------------------------
package cn.itcast_01;
import java.io.File;
/*
-
我们要实现IO操作,就必须知道银盘上文件的表现形式
-
而java就提供了一个类File供我们使用
-
File:文件和目录(文件夹)路径名的抽象表示形式
-
构造方法:
-
File(String pathname):根据一个路径得到一个File对象
-
File(String parent,String child):根据一个目录和一个子文件/目录得到一个File对象
-
File(File parent,String child):根据一个父File对象和一个子文件/目录得到File对象。
-
*/
public class FileDemo {
public static void main(String[] args) {
// File(String pathname):根据一个路径得到一个File对象
File file = new File(“E:\demo\a.txt”);// File(String parent,String child):根据一个目录和一个子文件/目录得到一个File对象 File file2 = new File("E:\\demo","a.txt"); // File(File parent,String child):根据一个父File对象和一个子文件/目录得到File对象。 File file3 = new File("E:\\demo"); File file4 = new File(file3,"a.txt"); // 以上三种方式运行结果一致
}
}
------------------------------------------知识点14--------------------------------------------------
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.tt // 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("mdik:"+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"); System.out.println("mkdirs:"+file7.mkdirs()); // 看下面东西 File file8 = new File("e:\\taobao.com\\a.txt"); System.out.println("mkdirs:"+file8.mkdirs());
}
}
------------------------------------------知识点15--------------------------------------------------
package cn.itcast_03;
import java.io.File;
import java.io.IOException;
/*
-
删除功能:public boolean delete()
-
注意:
-
1.如果创建文件夹忘记写盘符路径了,默认是在项目路径下。
-
2.Java中删除不走回收站
-
3.要删除文件夹,该文件夹中不能包含文件或者文件夹
-
*/
public class FileDemo {
public static void main(String[] args) throws IOException {
// 创建文件夹
// File file = new File(“e:\a.txt”);
// System.out.println(file.createNewFile());// 忘记写盘符 File file = new File("a.txt"); System.out.println(file.createNewFile()); File file2 = new File("aaa\\bbb\\ccc\\ddd"); System.out.println("mkdirs:"+file2.mkdirs()); // 删除功能 File file3 = new File("a.txt"); System.out.println("delete:"+file3.delete()); // 删除ddd文件件 File file4 = new File("aaa\\bbb\\ccc\\ddd"); System.out.println("delete:"+file4.delete()); // 删除aaa文件夹 File file5 = new File("aaa\\bbb\\ccc"); File file6 = new File("aaa\\bbb"); File file7 = new File("aaa"); System.out.println("delete:"+file5.delete()); System.out.println("delete:"+file6.delete()); System.out.println("delete:"+file7.delete());
}
}
------------------------------------------知识点16--------------------------------------------------
package cn.itcast_04;
import java.io.File;
/*
-
重命名功能
public boolean renameTo(File dest) -
注意:
-
以路径盘符开始:绝对路径
-
路径不以盘符开始:相对路径 a.txt
-
*/
public class FileDemo {
public static void main(String[] args) {
// 创建文件对象
File file = new File(“222.jpg”);// 需求:我们要改变这个文件的名称 // File newfile = new File("222.jpg"); // System.out.println("renameTo:"+file.renameTo(newfile)); File newfile = new File("e:\\mv.jpg"); System.out.println("renameTo:"+file.renameTo(newfile));
}
}
------------------------------------------知识点17--------------------------------------------------
package cn.itcast_05;
import java.io.File;
import java.io.IOException;
/*
-
判断功能:
-
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) throws IOException {
// 创建文件对象
File file = new File(“a.txt”);
file.createNewFile();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("isHidden:"+file.isHidden()); // false
}
}
------------------------------------------知识点18--------------------------------------------------
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()); // 毫秒值 1566818801618 Date d = new Date(1566818801618L); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = sdf.format(d); System.out.println(s);
}
}
------------------------------------------知识点19--------------------------------------------------
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 [] arrayStr = file.list(); for(String s:arrayStr){ System.out.println(s); } System.out.println("---------------------"); // public File[] listFiles(): 获取指定目录下的所有文件或者文件夹File数组。 File [] fileArray = file.listFiles(); for(File f:fileArray){ System.out.println(f.getName()); }
}
}
------------------------------------------知识点20--------------------------------------------------
package cn.itcast_08;
import java.io.File;
/*
-
判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称
-
分析:
-
1.封装e判断目录
-
2.获取该目录下所有文件或者文件夹的File的数组
-
3.遍历该File数组,得到每个File对象,然后判断
-
4.是否是文件
-
是:继续判断是否以.jpg结尾
-
是:就输出该文件名称
-
否: 不搭理
-
否:不搭理它
-
*/
public class FileDemo {
public static void main(String[] args) {
// 封装e判断目录
File file = new File(“e:\”);// 方法1 获取所有文件名数组 存在bug ,错误思路,没有判断文件的类型 String [] arrayStr = file.list(); for(String s:arrayStr){ if(s.contains(".jpg")){ // 不能用包含来判断,应该以“.jpg”结尾为条件 System.out.println(s); } } System.out.println("---------------"); // 方法2 获取所有文件对象 File [] fileArray = file.listFiles(); for(File f:fileArray){ if(f.isFile()){ if(f.getName().endsWith(".jpg")){ System.out.println(f.getName()); } } }
}
}
------------------------------------------知识点21--------------------------------------------------
package cn.itcast_08;
import java.io.File;
import java.io.FilenameFilter;
/*
-
判断e盘目录下是否后缀名为.jpg的文件,如果有就输出此文件名称
-
1.先获取所有的,然后遍历的时候,依次判断,如果满足条件就输出
-
2.获取的时候就已经满足条件的了,然后输出即可。
-
要实现这个效果,就必须学习一个接口:文件名称过滤器
-
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) { //System.out.println(dir+"---"+name); File file = new File(dir,name); boolean flag = file.isFile(); boolean flag2 = name.endsWith(".jpg"); return flag&&flag2; } }); for(String s:strArray){ System.out.println(s); }
}
}
package cn.itcast_09;
import java.io.File;
/*
-
需求:把E:\评书\三国演义下面的视频名称修改为
-
00?_介绍.avi
-
00?_介绍.avi
-
思路:
-
1.获取所有文件对象
-
2.文件对象进行重命名
-
*/
public class FileDemo {
public static void main(String[] args) {
// 创建文件对象
File file = new File(“E:\评书\三国演义”);
File [] fileArray = file.listFiles();
for(File f:fileArray){
//System.out.println(f.getName());
String s = f.getName();
int index1 = s.indexOf("");
// System.out.println(index);
String NumString = s.substring(index1+1,index1+4);
// System.out.println(NumString);
int index2 = s.lastIndexOf("");
// System.out.println(index2);
String introduction = s.substring(index2+1,s.length());
//System.out.println(introduction);
String newname = NumString.concat("_").concat(introduction);
//System.out.println(newname);
File newfile = new File(file,newname);
f.renameTo(newfile);
}// 输出处理后的文件 File file2 = new File("E:\\评书\\三国演义"); File [] fileArray2 = file2.listFiles(); for(File f:fileArray2){ System.out.println(f.getName()); }
}
}