Flie工具包练习

该博客内容涉及一系列文件操作方法的实现,包括创建文件夹、判断文件类型、查询文件、文件拷贝和删除等。提供了详细的代码示例,如创建文件夹、查找指定扩展名的文件、拷贝文件到指定位置并验证拷贝结果以及删除文件等。测试类对这些功能进行了实际调用和验证。
摘要由CSDN通过智能技术生成

设计文件工具包
1、设计创建文件夹方法,入参为路径和文件夹名称,返回值为文件夹全路径
2、设计判断是否为文件夹方法,入参为路径,返回值为是否为文件夹
3、设计查询文件夹下所有文件方法,入参为路径,返回值为所有文件的集合(文件夹中有文件和文件夹)
4、设计查询文件夹下所有文件方法,入参为路径、扩展名,返回值为相同类型文件的集合
5、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径,返回值判断对应路径下是否存在该文件
6、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径、拷贝后的文件名,返回值判断对应路径下是否存在该文件
7、设计文件删除方法,入参为被删除文件路径,返回值为是否删除成果

编写测试类,测试相关功能

package WanZiXi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ShanHai:
 * @version 创建时间:2021年8月24日 下午4:24:31
 */
public class bao {
	/**
	 * 1、设计创建文件夹方法,入参为路径和文件夹名称,返回值为文件夹全路径
	 * 
	 * @throws IOException
	 */
	public String chuangJian(String name, String path) throws IOException {
		String newPath = "";
		File f = new File(path);
		f.createNewFile();
		newPath = path + name;
		System.out.println(newPath);
		return newPath;
	}

	/**
	 * 2、设计判断是否为文件夹方法,入参为路径,返回值为是否为文件夹
	 */
	public boolean panDuan(String path) {
		File f = new File(path);
		if (f.isDirectory()) {
			System.out.println("是文件夹" + true);
			return true;
		} else {
			System.out.println("不是文件夹" + false);
			return false;
		}
	}

	/**
	 * 3、设计查询文件夹下所有文件方法,入参为路径, 返回值为所有文件的集合(文件夹中有文件和文件夹)
	 * 
	 * @param path
	 */
	public List chaXun(String path) {
		List<String> l = new ArrayList<String>();
		File f = new File(path);
		File[] allf = f.listFiles();
		for (File s : allf) {
			l.add(s.toString());
		}
		System.out.println(l);
		return l;
	}

	/**
	 * 4、设计查询文件夹下所有文件方法,入参为路径、扩展名,返回值为相同类型文件的集合
	 */
	public void chaXun2(String path, String tname) {
		// 获取拓展名
		File f = new File(path);
		File[] file = f.listFiles();// file为所有文件
		List<String> l = new ArrayList<String>();// file2为类名相同的文件
		// tname = path.substring(i + 1);// 后缀名
		for (File f1 : file) {// 遍历
			int i = f1.getName().lastIndexOf(".");// 如果不是-1,则表示改字符存在
			if (i != -1) {
				System.out.println(f1.getName().substring(i + 1));
				String f2 = f1.getName().substring(i + 1);// 后缀名
				if (f2.equals(tname)) {
					l.add(f1.toString());
				}
			}
		}
		System.out.println(l);
	}

	/**
	 * 5、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径, 返回值判断对应路径下是否存在该文件
	 * 
	 * @param file
	 * @param path
	 * @throws IOException
	 */
	public boolean kaoBei(String file, String path) throws IOException {
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr, 1024);
		FileWriter fw = new FileWriter(path);
		BufferedWriter bw = new BufferedWriter(fw);
		String i = "";
		while ((i = br.readLine()) != null) {
			bw.write(i.toString());
		}
		File f = new File(path);
		boolean b = f.exists();
		if (b) {
			System.out.println("存在该文件");
		} else {
			System.out.println("不存在该文件");
		}

		bw.close();
		fw.close();
		br.close();
		fr.close();
		return b;

	}

	/**
	 * 6、设计文件拷贝方法,入参为被拷贝文件、拷贝到的文件路径、拷贝后的文件名, 返回值判断对应路径下是否存在该文件
	 * 
	 * @param file
	 * @param path
	 * @param name
	 * @throws IOException
	 */
	public boolean kaoBei2(String file, String path, String name) throws IOException {
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr, 1024);
		FileWriter fw = new FileWriter(path);
		BufferedWriter bw = new BufferedWriter(fw, 1024);
		String i = "";
		while ((i = br.readLine()) != null) {
			bw.write(i.toString());
		}
		File f = new File(path);
		boolean b = f.exists();
		if (b) {
			System.out.println("存在该文件");
		} else {
			System.out.println("不存在该文件");
		}
		fr.close();
		br.close();
		bw.close();
		fw.close();
		return b;
	}

	/**
	 * 设计文件删除方法,入参为被删除文件路径,返回值为是否删除成果
	 * 
	 * @param path
	 */
	public boolean shanChu(String path) {
		File f = new File(path);
		boolean a = f.delete();
		if (a) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
		return a;
	}
}

package WanZiXi;

import java.io.IOException;

/**
*@author ShanHai:
*@version 创建时间:2021年8月21日 下午4:24:43
*/
public class Jtest {
	public static void main(String[] args) throws IOException {
		bao ba=new bao();//实例化包
		
		System.out.println("********第1题****");
		//1、调用chuangJian方法
		ba.chuangJian("test1.txt", "C:\\Users\\Lenovo\\Documents\\Hello\\test1.txt");
		
		System.out.println("********第2题****");
		//2、调用panDuan方法
		ba.panDuan("C:\\Users\\Lenovo\\Documents\\Hello\\test1.txt");
		
		System.out.println("********第3题****");
		//3、调用chaXun方法
		ba.chaXun("C:\\Users\\Lenovo\\Documents\\Hello");
		
		System.out.println("********第4题****");
		//4、调用chaXun2方法
		ba.chaXun2("C:\\Users\\Lenovo\\Documents\\Hello", "txt");
		
		System.out.println("********第5题****");
		//5、调用kaoBei方法
		ba.kaoBei("C:\\Users\\Lenovo\\Documents\\Hello\\test1.txt", "C:\\Users\\Lenovo\\Documents\\Hello\\test2.txt");
		
		System.out.println("********第6题****");
		//6、调用kaoBei2方法
		ba.kaoBei2("C:\\Users\\Lenovo\\Documents\\Hello\\test2.txt", "C:\\Users\\Lenovo\\Documents\\Hello\\test3.txt","test3.txt");

		System.out.println("********第7题****");
		//7、调用shanChu方法
		ba.shanChu("C:\\Users\\Lenovo\\Documents\\Hello\\test1.txt");
	}
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShanHai山海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值