Java IO工具小组件 文件增删改查,加密,解密,复制,文件目录删除

在删除文件目录部分被卡住,需要递归判断该文件是不是文件夹,是,就往下取子目录判断,不是文件夹就删除。
刚开始使用List方法,达不到需求,原因是在获取子文件的名称往下递归时,并没有加上父路径,所有判断一直在当前操作层查找,后改用ListFiles方法,解决该问题

import java.io.*;
import java.util.*;

public class IOUtil{
	public static void main(String[] main){
		try{
		//	fileCopy("io.txt","ioCopy.txt");
		//  fileEncrypt("io.txt");
		//  fileDecrypt("io.txt");
		//	String[] strs = fileReadMultiLine("io.txt");
		// 遍历数组
		//	for(int i=0;i<strs.length;i++){
		//		System.out.println(strs[i]);
		// String[] strs = {"123","abc","有志者事竟成"};
		// fileWriteMultiLine(strs,"iotext.txt");
		//	fileContentAppend("三千越甲可吞吴","io.txt");
		// fileContentPrepend("加油!","io.txt");
		// fileContentCopy("io.txt");
		// fileContentReplace("io.txt","三千","五万");
		  fileDelete("新建文件夹1");
		}catch(Exception e){
			e.printStackTrace();  //好多博客都说别用这个容易造成锁死?
		}
	}
	/**
	*文件复制
	*@param String srcFile,String tarFile
	*/
	public static void fileCopy(String srcFile,String tarFile)throws IOException{
		//创建File
		File f = new File(srcFile);
		//创建FileInputStream的对象
		FileInputStream fis = new FileInputStream(f);
		//创建FileOutputStream的对象
		FileOutputStream fos = new FileOutputStream(tarFile);
		//定义数组,数组长度为文本文件长度
		byte[] b = new byte[(int)f.length()];
		//写出内容到tarFile文件
		while(true){
			int rtn = fis.read(b);   //rtn什么作用?
			System.out.println(rtn);
			if(rtn==-1){
				fos.close();
				fis.close();
				break;
			}else{
				fos.write(b);
			}
		}
	}
	
	/**
	*文件加密
	*@param String srcFile
	*/
	public static void fileEncrypt(String srcFile) throws IOException{
		RandomAccessFile rf = null;
		File f = new File(srcFile);
		rf = new RandomAccessFile(f,"rw");
		byte[] b = new byte[(int)f.length()];
		//读入文本io.txt的内容
		rf.read(b);
		//进行文件加密
		for(int i=0;i<b.length;i++){
			b[i] = (byte)(b[i]+12);
		}
		//将指针移到开头
		rf.seek(0);
		//写出内容到io.txt
		rf.write(b);
		rf.close();
	}
	
	/**
	*文件解密
	*@param String srcFile
	*/
	public static void fileDecrypt(String srcFile) throws IOException{
		RandomAccessFile rf = null;
		File f = new File(srcFile);
		rf = new RandomAccessFile(f,"rw");
		byte[] b = new byte[(int)f.length()];
		//读入文本io.txt的内容
		rf.read(b);
		//进行文件加密
		for(int i=0;i<b.length;i++){
			b[i] = (byte)(b[i]-12);
		}
		//将指针移到开头
		rf.seek(0);
		//写出内容到io.txt
		rf.write(b);
		rf.close();
	}
	
	/**
	*读取文件中多行文本并解析为字符串数组返回
	*@param String srcFile
	*@return String[]
	*/
	public static String[] fileReadMultiLine(String srcFile) throws IOException{
		BufferedReader br1 = new BufferedReader(new FileReader(srcFile));
		//记录文件有几行文本内容
		int line = 0;
		//判断有多少行
		while(true){
			if(br1.readLine() != null){
				//行数+1
				line++;
			}else{
				br1.close();
				break;
			}
		}
		BufferedReader br2 = new BufferedReader(new FileReader(srcFile));
		//创建字符串数组
		String[] strs = new String[line];
		//赋值给字符串数组
		for(int i=0;i<strs.length;i++){
			strs[i] = br2.readLine();
		}
		br2.close();
		//返回字符串数组
		return strs;
	}
	
	/**
	*把一个字符串数组中的多个字符串分行写入文本文件
	*@param String[] textArray,String tarFile
	*/
	public static void fileWriteMultiLine(String[] textArray,String tarFile) throws IOException{
		BufferedWriter bw = new BufferedWriter(new FileWriter(tarFile));
		//写出字符串数组的值
		for(int i=0;i<textArray.length;i++){
			bw.write(textArray[i]);
			if(i != (textArray.length-1)){
				//转行
				bw.newLine();
			}
		}
		bw.close();
	}
	
	/**
	*把一个字符串追加到一个文本文件内容后
	*@param String text,String srcFile
	*/
	//个人思路:先读取文件,注意要判断文本分行的情况
	public static void fileContentAppend(String text,String srcFile) throws IOException{
		RandomAccessFile ca = null;
		File f = new File(srcFile);
		ca = new RandomAccessFile(f,"rw");
		//字符串转换为byte数组
		String  str = text;
		byte[] b = str.getBytes();
		ca.seek(ca.length());
		ca.write(b);
		ca.close();
	}
	
	/**
	*把一个字符串插入到一个文本文件内容前
	*@param String text,String srcFile
	*/
	public static void fileContentPrepend(String text,String srcFile) throws IOException{
		File f = new File(srcFile);
		FileInputStream fis = new FileInputStream(f);
		byte[] b1 = new byte[(int)f.length()];
		fis.read(b1);
		RandomAccessFile fa = null;
		fa = new RandomAccessFile(f,"rw");
		//字符串转换为byte数组
		String  str = text;
		byte[] b2 = str.getBytes();
		fa.seek(0);
		fa.write(b2);
		fa.write(b1);
		fa.close();
	}
	
	/**
	*复制文件内容并追加到原文件内容后
	*@param String srcFile
	*/
	public static void fileContentCopy(String srcFile) throws IOException{
		File f = new File(srcFile);
		RandomAccessFile fa = new RandomAccessFile(f,"rw");
		byte[] b1 = new byte[(int)f.length()];
		fa.read(b1);
		fa.seek(f.length());
		fa.write(b1);
		fa.close();
		
	}
	
	/**
	*替换文本指定内容
	*@param String srcFile,String oldContent,String newContent
	*/
	public static void fileContentReplace(String srcFile,String oldContent,String newContent) throws IOException{
		File f = new File(srcFile);
		FileReader f1 = new FileReader(f);
		char[] c = new char[(int)f.length()];
		String s = "";
		f1.read(c);
		int len = (int)f.length();
		for(int i=0;i<len;i++){
			s += String.valueOf(c[i]);
		}
		//String s = String.valueOf('c');
		//String str = String.valueOf(new c[]);
		System.out.println(s);
		String str1 = s.replaceAll(oldContent,newContent);
		System.out.println(str1);
		FileWriter f2 = new FileWriter(f);
		f2.write(str1);
		f2.close();
	}
	/**
	*强制删除一个文件(夹)(如果是文件夹,删除里面的所有内容)
	*@param String srcFile
	*/
	public static void fileDelete(String srcFile) throws IOException{
		File f = new File(srcFile);
		if(f.isFile()){
			f.delete();
		}
		else{
			
			deleteDirectory(f);
			//deleteDirectory(srcFile);
		}	
	}
	public static void deleteDirectory(File f) throws IOException{
		//File f = new File(srcFile);
		System.out.println(f.getPath());
		if(f.isFile()){
			f.delete();
			System.out.println("删除成功");
		}else{
			
			File[] str = f.listFiles();
			System.out.println(str);
			for(int i = 0; i < str.length; i++){
				if(str[i].isFile()){
					str[i].delete();
				}else{					
					deleteDirectory(str[i]);
				}				  
			}
			
			f.delete();
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值