异常体系和File类

一、异常


Java异常是Java程序运行过程中出现的非正常情况,异常在程序中如果不做处理,会导致程序中途瘫痪,不能正常完成特定功能,异常一般情况是需要通过Java异常处理机制给屏蔽或者处理掉


异常对象产生后,会根据代码对异常的处理机制进行操作,其中异常处理机制包括捕获(catch)异常和抛出(throw)异常两种,如果没有异常处理操作,则java程序会中断执行




1.捕获异常

try{
//可能出现异常的代码部分
 }catch(异常类型 异常变量){
//执行异常操作
}catch(异常类型 异常变量){
//执行异常操作
 }…
finally{
    //处理完try与catch操作后的收尾操作,异常统一出口

finally关键字,在进行异常处理之后,在异常的处理格式中还有一个finally语句,此语句将作为异常的统一出口,不管是否产生异常, 最终都要执行此段代码


2.抛出异常

此方法把异常问题抛出给调用此方法的上一级函数当中,并调用出自行处理异常

public void test() throws 异常类型{
//存在异常的代码块
}


二、File类


1.文件查找

public void search(String path,String key){
		File file = new File(path);
		if(file.exists()){		//判断文件(夹)是否存在
			if(file.isDirectory()){
				//如果是文件夹
				Test.folderNum++;
				File[] listFiles = file.listFiles();//返回文件夹内的子文件与子文件夹的数组
				for (int i = 0; i < listFiles.length; i++) {
					File file1 = listFiles[i];
					search(file1.getAbsolutePath(),key);
				}
			}
			else{
				//如果是文件
				String name = file.getName();
				if(name.contains(key)){             //是否包含关键字
					String absolutePath = file.getAbsolutePath();
					Test.fileNum++;
					list.add(absolutePath);
				}
			}
		}
	}


2.文件的读与写

字节流:

FileInputStream

public void fileInput(String path){
		File file = new File(path);
		if(file.exists()){        //判断文件是否存在
			if(file.isFile()){
			   //验证是否为文件
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(file);
					byte[] b = new byte[1024];//缓存数组(1k)
					int length = -1;
					while((length = fis.read(b))!= -1){
						String result = new String(b,0,length);
						System.out.println(result);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					try {
						if(fis != null)
							fis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
			}
		}
	}


FileOutputStream

public void fileOutput(String Destination,String content,boolean isAppend ){
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(Destination,isAppend);
			byte[] bytes = content.getBytes();
			fos.write(bytes);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos != null)
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


字符流:
FileReader

public void fileInput(String path){
				FileReader fis = null;
				try {
					fis = new FileReader(path);
					char[] b = new char[1024];//缓存数组(1k)
					int length = -1;
					while((length = fis.read(b))!= -1){
						String result = new String(b,0,length);
						System.out.println(result);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					try {
						if(fis != null)
							fis.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
			}

FileWriter

public void fileOutput(String Destination,String content,boolean isAppend ){
		FileWriter fos = null;
		try {
			fos = new FileWriter(Destination,isAppend);
			fos.write(content);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(fos != null)
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

3.文件复制

public void copyFile(String source,String destination){
		File fileSrc = new File(source);
		File fileDest = new File(destination);
		if(fileSrc.exists()){        //判断文件夹是否存在
			if(fileSrc.isFile()){	 //判断是否为文件
				if(!fileDest.exists()){	
					File parent = new File(fileDest.getParent());
					parent.mkdirs();
				}
				BufferedReader br = null;
				BufferedWriter bw = null;
				
				try {				
					br = new BufferedReader(new FileReader(fileSrc));
					bw = new BufferedWriter(new FileWriter(fileDest));
									
					String length = null;
					while((length = br.readLine())!= null){
						bw.write(length);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					try {
						if(br != null)
							br.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					try {
						if(bw != null)
							bw.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
				
			}
			else{
				copyFolder(source,destination);
			}
		}
	}
	
	public void copyFolder(String srcPath,String destPath){
		File fileSrc = new File(srcPath);
//		File fileDest = new File(destPath);
		if(fileSrc.exists()){        //判断文件夹是否存在
			if(fileSrc.isFile()){	 //判断是否为文件
				copyFile(srcPath,destPath);
			}else{
				File[] listFiles = fileSrc.listFiles();
				for (int i = 0; i < listFiles.length; i++) {
					File childFile = listFiles[i];
					//递归
					String srcPathChild = childFile.getAbsolutePath();
					String child = childFile.getName();
					String destPathChild = destPath + "/" +child;
					copyFolder(srcPathChild,destPathChild);
				}
 			}
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值