Java遍历文件夹,重命名文件,删除文件,复制文件等操作

package mycompare;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

public class arrcompare {

	public static void main(String args[]) throws Exception {
		MyFile.ListFiles("E:\\学习资料\\ex");		
		MyFile.DeleteFile("E:\\学习资料\\ex\\b.txt");
		MyFile.Rename("E:\\学习资料\\ex\\c.txt", "E:\\学习资料\\ex\\a.txt");
		MyFile.FileCopy("E:\\学习资料\\ex\\a.txt", "E:\\学习资料\\ex\\f.txt");
		MyFile.FileCopy(new String[]{"E:\\学习资料\\ex\\d.txt","E:\\学习资料\\ex\\g.txt"});
	}
}

class MyFile
{
    /**
     * 遍历文件夹中文件
     * @param dicpath
     * @return
     */
	public static boolean ListFiles(String dicpath) {
		File file=new File(dicpath);
		
		if (!file.isDirectory()) {
			System.out.println("必须是文件夹!");
			return false;
		}
		
		if (!(file.exists() && file.isDirectory())) {
			System.out.println("目录不存在!");
			return false;
		}
		
		File[] files=file.listFiles();
		for (File file2 : files) {
			System.out.println(file2.getName());
		}
		return true;
	}
    /**
     * 删除文件(包括文件夹)
     * @param filepath
     * @return
     */
    public static boolean DeleteFile(String filepath) {
		File file=new File(filepath);
		if (file.exists()) {
			file.delete();
			return true;
		}
		else {
			return false;
		}
	}
    /**
     * 文件重命名
     * @param oldname
     * @param newname
     * @return
     */
    public static boolean Rename(String oldname,String newname) {
		File newfile=new File(newname);
		File oldfile=new File(oldname);
    	if (newfile.exists()) {
			System.out.println("已经存在"+newname);
			return false;
		}
    	if (!oldfile.exists()) {
			System.out.println("不存在目标文件"+oldname);
			return false;
		}
    	oldfile.renameTo(newfile);
    	return true;
	}
    /**
     * 复制文件,比另一个复制方式要快
     * @param oripath
     * @param despath
     * @return
     * @throws IOException
     */
    public static boolean FileCopy(String oripath,String despath) throws IOException {
		FileInputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		FileChannel in=null;
		FileChannel out=null;
		try
		{
			fileInputStream=new FileInputStream(oripath);
			fileOutputStream=new FileOutputStream(despath);
			in=fileInputStream.getChannel();//得到文件通道
			out=fileOutputStream.getChannel();
			in.transferTo(0, in.size(), out);//连接in与out通道,然后写入out通道
			return true;
		}
		catch (IOException e) {			
			e.printStackTrace();
			return false;
		}
		finally {
			fileInputStream.close();
			fileOutputStream.close();
			in.close();
			out.close();
		}
	}
    /**
     * 文件复制,比另一种复制方式要慢
     * @param strings
     * @throws IOException
     */
    public static void FileCopy(String[] strings) throws IOException{
		if (strings.length!=2) {
			System.out.println("请输入俩个文件路径!");
			return;
		}    	
		InputStream fileInputStream=null;
		FileOutputStream fileOutputStream=null;
		try {
			fileInputStream=new FileInputStream(strings[0]);
			fileOutputStream=new FileOutputStream(strings[1]);
			int len=0;
			byte[] buf=new byte[1024];
			while((len=fileInputStream.read(buf))!=-1)
			{
				fileOutputStream.write(buf,0,len);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		finally {
			fileInputStream.close();
			fileOutputStream.close();
		}
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值