File文件的操作以及异常处理

一、判断文件是目录还是正式文件、文件系统
1、文件的全名包含文件的路径名和扩展名,单独命名文件时用的是其扩展名;
2、同一目录下不能存在同名的目录和真实文件(系统主要有目录型文件、真实文件,和不需要计算的如虚拟内存文件,其中真实文件可以没有扩展名;
3、相对路径:不以路径分割符开头的文件路径
绝对路径:以根目录开头的路径
4、目录分割符和路径分割符在windows、Linux平台上不同
[b] window:"\",";"[/b]
[b] Linux:"/",":"[/b]

二、File类在java.io包中,可以通过File类来创建文件对象,并进行删除文件等
File类中的常用函数:
1、构造器
File([color=red]File[/color] parent,String child)
在一个父文件对象下创建一个名字为child的文件对象
File(String pathname)
创建一个指向pathname的File对象
File([color=red]String parent[/color],String child)
以一个名为parent字符串为父目录名,创建一个指向名为child的文件对象
当使用File的构造器创建File对象时,[color=red]仅仅是在JVM内存中生成了一个File类型的对象[/color]——此时与磁盘上的文件无关联,只有当调用这个对象的其他方法时,这个对象才会关联到即指向磁盘上的某个文件
2、重要方法
[color=red]boolean exists()[/color]判断File对象指向的文件是否存在
[color=red]String getName()[/color]得到不带路径的文件名
[color=red]boolean isDirectory()[/color]判断File对象指向的文件是否是目录
[color=red]boolean isFile()[/color]判断File对象指向的文件是否是文件
[color=red]long length()[/color]得到File对象指向的文件长度,以byte计,如若指向目录,返回0
[color=red]File [] listFiles()[/color]将File对象指向的文件的下一级的所有目录和真实文件作为一个数组返回,其中真实文件返回值是null
static File[] listRoots()
可通过File类之间调用
window:将每一个盘作为数组中的一个File对象元素返回
Linux:返回值为1(单文件系统,只有一个Root根)
boolean delete()
从磁盘上删除File对象指向的文件
boolean createNewFile()
根据File对象的名字在磁盘上创建一个新的真实文件;若已经存在同名文件则覆盖,若存在同名目录则抛出异常,创建失败

三、文件搜索器的制作
1、文件搜索器的窗体类(同制作画板的窗体类一样,是JFrame的子类)
代码如下:
public class fileSearch extends JFrame{
/**
* 主函数
*/
public static void main(String [] args){
fileSearch file = new fileSearch();
file.listRoots();
file.testFile("测试文件");
file.showUI();
}
/**
* 列出机器上所有的根目录
*/
public void listRoots(){
//直接调用File类的static方法
File[] fa= File.listRoots();
System.out.println("磁盘上目录个数是:"+fa.length);
for(int i=0;i<fa.length;i++){
System.out.println("第"+i+"个目录:"+fa[i].getAbsolutePath());

}
}
/**
* 测试文件创建或删除
*/
public void testFile(String filename){
//通过传入的名字构建File对象
File fl = new File(filename);
//判断文件是否存在
if(fl.exists()){
//如果是一个目录
if(fl.isDirectory()){
System.out.println("这是一个目录:"+fl.getAbsolutePath());
}
//如果是一个文件
if(fl.isFile()){
//打印文件的长度
prinFileAttr(fl);
//删除文件
fl.delete();
String theName = fl.getName();
String absPath = fl.getAbsolutePath();
System.out.println("文件已删除!文件名:"+theName+" 绝对路径:"+absPath);
}
}
else{
[color=red]try[/color]{
fl.createNewFile();
System.out.println("文件已创建!"+fl.getAbsolutePath());
//打印文件长度
prinFileAttr(fl);
}[color=red]catch(Exception ef)[/color]{
System.out.println("创建文件失败!");
}[color=red]//异常处理[/color]

}

}


/**
* 打印文件
*/
public void prinFileAttr(File fl){
System.out.println("文件长度是:"+fl.length());
}
/**
* 文件搜索器的界面显示
*/

public void showUI(){
this.setSize(600,500);
this.setTitle("我的文件搜索器");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
//设置流式布局
this.setLayout(new FlowLayout());
//创建一个标签对象
JLabel one = new JLabel("要搜索的文件或目录名");
this.add(one);
JTextField txtFile= new JTextField(10);
this.add(txtFile);
JLabel two = new JLabel("搜索范围");
this.add(two);
//得到一个数组
String [] array = getRoots();
JComboBox cbItem = new JComboBox(array);
this.add(cbItem);

JButton btnsta= new JButton("START");
btnsta.setActionCommand("Start");
JButton btnsto = new JButton("STOP");
btnsto.setActionCommand("Stop");
this.add(btnsta);
this.add(btnsto);
//初始化行和列
JTextArea txtMessage = new JTextArea(20,50);
//滚动面板
JScrollPane pane = new JScrollPane(txtMessage);
//设置面板大小
pane.setSize(new Dimension(550,400));
this.add(pane);


this.setVisible(true);
//创建查找文件事件处理类的监听器对象
QueryListener ql = new QueryListener(txtFile,cbItem,txtMessage);
//给按钮添加监听器
btnsta.addActionListener(ql);
btnsto.addActionListener(ql);

}
/**
* 获取所以的根目录
*/
public String [] getRoots(){
//使用File对象直接调用listRoots方法,得到当前系统下的所有根目录。windows的根目录是磁盘,Linux只有一个root
File[] fl = File.listRoots();
String []array = new String [fl.length];
for(int i=0;i<fl.length;i++){
//得到目录路径
String path = fl[i].getAbsolutePath();
//将路径存入数组
array[i]=path;
System.out.println("目录:"+path);
}
return array;

}

}
2、通过递归算法查找某个目录下的所有文件
代码如下:
public class FileDisplay {
//主函数
public static void main(String [] args){
FileDisplay fd = new FileDisplay();
String dirName = "src";
int count = fd.printF("src");
System.out.println(dirName+"目录下共找到"+count+"个文件!");
}
/**
* 打印dirName下的所有文件和目录的名字
*/
public int printF(String dirName){
int filecount = 0;
File dirFile = new File(dirName);
File[] subFile = dirFile.listFiles();//所有目录和真实文件
if(null==subFile||subFile.length==0)
return 0;//为空目录或真实文件
//遍历子目录
for(int i=0;i<subFile.length;i++){
if([color=red]subFile[i].isDirectory()[/color]){
String subDir = subFile[i].getAbsolutePath();
System.out.println("目录:"+subDir);
[color=red]filecount += printF(subDir);//若仍是目录,则递归[/color] }
if([color=red]subFile[i].isFile()[/color]){
filecount++;
String fileName=subFile[i].getAbsolutePath();
System.out.println("文件:"+fileName);
}
}
return filecount;
}
}

四、异常处理
常见的异常关键字:
1、try{
可能抛出异常的代码...
//如果没有出现异常执行的代码A
A:其他计算代码
} catch(Exception ef){
//如果出现异常后执行的代码B
B:出现异常后执行的代码
}[color=red]finally[/color]{
//不管是否出现异常,都将执行finally中的语句
}
当程序运行到try部分时,若代码没有出现异常,则将try部分的代码(A)执行完毕后跳过catch部分的代码;若try部分的代码出现异常则不执行(A),执行catch中的(B)
eg:
try{
//创建文件
temFile.createNewFile();
System.out.println("文件已经创建!"+temFile.getAbsolutePath);
}catch(Exception ef){
ef.printStackTrace();//[color=red]让异常对象打印出错原因 [/color]}
2、throw
自定义抛出异常的类型
throw new Exception("自定义异常!");
如果方法体中使用throw关键字抛出了一个Exception类型(或其子类)的对象,在方法中必须使用throws关键字声明
(throws关键字后面异常类的名字必须是throw抛出异常对象的类型或其父类的类型)
3、throws
用来抛出异常
eg:
public void test()throws Exception{}
4、在一段代码中如果出现多种异常:有两种处理方式:
1)直接抛出Exception异常,因为所有的异常类都是Exception的子类
2)可以加上多个catch语句,不同的异常,交给不同catch进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值