JavaIO流学习成果(工具类)

这两天又把IO相关的过了一遍加深了一下印象,下面是自己写的工具类欢迎批评指正

package com.wangzic.io_tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class Mt_IO {
	//文件复制
	public static void copyFile(String sourcepath,String targetpath){
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			if(!new File(sourcepath).exists()){
				System.out.println("未找到此文件:"+sourcepath);
				return ;
			}
			int index = targetpath.lastIndexOf("/")!=-1?targetpath.lastIndexOf("/"):targetpath.lastIndexOf("\\");
			String path =targetpath.substring(0, index);
			if(!new File(path).exists()){
				new File(path).mkdirs();
			}
			fis = new FileInputStream(sourcepath);
			fos = new FileOutputStream(targetpath);
			
			byte[] buf = new byte[1024];
			int len =0;
			while((len=fis.read(buf))!=-1){
				fos.write(buf,0,len);
			}
		} catch (Exception e) {
			throw new RuntimeException("文件复制失败"+e);
		}finally{
			try {
				if(fis!=null){
					fis.close();
				}
				if(fos!=null){
					fos.close();
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}
	//移动文件
	public static void moveFile(String sourcepath,String targetpath){
		copyFile(sourcepath, targetpath);
		File file =new File(sourcepath);
		if(file.exists()){
			file.delete();
		}
	}
	//列出文件列表
	public static void showdir(File dir,int level){
		System.out.println(getLevel(level)+dir.getName());
		level++;
		File[] files = dir.listFiles();
		for(File f:files){
			if(f.isDirectory()){
				showdir(f,level);
			}else{
				System.out.println(getLevel(level)+f.getName());
			}
		}
	}
	public static String getLevel(int level){
		StringBuilder  sb = new StringBuilder();
		sb.append("|——");
		for(int i =0;i<level;i++){
			sb.insert(0, "  ");
		}
		return sb.toString();
	}
	//2进制转换
	public static String tobin(int nub){
		List<String> bin = new ArrayList<String>(); 
		StringBuilder builder = new StringBuilder();
		while(nub>0){
			bin.add(nub%2+"");
			nub = nub/2;
		}
		for(int i = bin.size()-1;i>-1;i--){
			builder.append(bin.get(i));
		}
		return builder.toString();
	}
	//遍历一个文件夹中所有的某种文件将其放进集合中
	public static void fileToList(File dir,List<File> list,String filetype ){
		File[] files = dir.listFiles();
		for(File file : files){
			if(file.isDirectory()){
				fileToList(file,list,filetype);
			}else{
				if(file.getName().endsWith(filetype)){
					list.add(file);
				}
			}
		}
	}
	//将集合中的文件路径放进txt文本中
	public static void listToTxt(File dir,String filetype){
		List<File> list = new ArrayList<File>();
		fileToList(dir,list,filetype);
		try {
			FileWriter fw = new FileWriter(dir+"/文件路径列表.txt");
			BufferedWriter bufw = new BufferedWriter(fw);
			int i =0;
			for(File file :list){
				i++;
				bufw.write(i+":"+file.getPath());
				bufw.flush();
				bufw.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	//判断文件路径是否存在不存在则创建
	public static File createpath(String path){
		int index = path.lastIndexOf("/")!=-1?path.lastIndexOf("/"):path.lastIndexOf("\\");
		String cpath =path.substring(0, index);
		File file  = new File(cpath);
		if(!file.exists()){
			file.mkdirs();
		}
		return new File(path);
	}
	
	
	//多个文本文件合并为一个文件
	public static void filesToOne(List<String> filelist,String savepath){
		
		Vector<FileInputStream> vector = new Vector<FileInputStream>();
		try {
			for(String str :filelist){
				if(new File(str).exists()){
					vector.add(new FileInputStream(str));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Enumeration<FileInputStream> enumeration = vector.elements();
		SequenceInputStream sis = null;
		FileOutputStream fos = null;
		try {
			sis = new SequenceInputStream(enumeration);
			fos = new FileOutputStream(createpath(savepath));
			byte[] buf = new byte[1024];
			int len = 0;
			while((len = sis.read(buf))!=-1){
				fos.write(buf,0,len);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(sis!=null)
					sis.close();
				if(fos!=null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		}
		
	}
	//将一个文件切割成多个文件
	public static void splitFile(String sourcefile,int size){
		File file  = new File(sourcefile);
		if(file.exists()){
			FileInputStream fis = null;
			FileOutputStream fos = null;
			try {
				fis = new FileInputStream(file);
				int len = 0;
				int count = 2;
				String path = file.getName();
				int index2 = path.lastIndexOf(".");
				String name = file.getParent()+"\\"+path.substring(0,index2)+"_";
				if(size>1024*1024){
					byte[] buf  = new byte[1024*1024];
					String nowname = name+"1.part";
					fos = new FileOutputStream(nowname);
					while((len =fis.read(buf))!=-1){
						fos.write(buf, 0, len);
						if(new File(nowname).length()>=size){
							fos.close();
							nowname  = name+(count++)+".part";
							fos = new FileOutputStream(nowname);
						}
					}
				}else{
					byte[] buf  = new byte[size];
					count =1;
					while((len =fis.read(buf))!=-1){
						fos = new FileOutputStream(name+(count++)+".part");
						fos.write(buf, 0, len);
						fos.close();
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(fis!=null){
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}else{
			System.out.println("未找到此文件");
			return;
		}
		
		
	}
	//批量重命名
	public static void batchRename(String path,String name){
		File file = new File(path);
		if(!file.exists()){
			return ;
		}
		File[] files =file.listFiles();
		int i = 1;
		for(File f :files){
			File rf =null;
			if(f.isFile()){
				String type = f.getName().substring(f.getName().lastIndexOf("."),f.getName().length());
				rf = new File(path,name+(i++)+type);
			}else if(f.isDirectory()){
				rf = new File(path,name+(i++));
			}
			if(rf!=null){
				f.renameTo(rf);
			}
		}
	}
	//复制多级文件夹
	public static void copyFolder(File srcFile, File destFile)
			throws IOException {
		// 判断该File是文件夹还是文件
		if (srcFile.isDirectory()) {
			// 文件夹
			File newFolder = new File(destFile, srcFile.getName());
			newFolder.mkdir();

			// 获取该File对象下的所有文件或者文件夹File对象
			File[] fileArray = srcFile.listFiles();
			for (File file : fileArray) {
				copyFolder(file, newFolder);
			}
		} else {
			// 文件
			File newFile = new File(destFile, srcFile.getName());
			copyFile(srcFile.getAbsolutePath(), newFile.getAbsolutePath());
		}
	}

	public static void main(String[] args) throws IOException {
//		copyFile("D:/Libraries/Pictures/3b21.jpg", "D:/Libraries/Pictures/3.png");
//		moveFile("D:/Libraries/Pictures/3b2.jpg", "D:/Libraries/Pictures/images/3.png");
//		listToTxt(new File("D:/Libraries/Pictures"), ".jpg");
//		List<String> filelist = new ArrayList<String>();
//		filelist.add("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_1.part");
//		filelist.add("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_2.part");
//		filelist.add("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_3.part");
//		filelist.add("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_4.part");
//		filelist.add("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_5.part");
//		filesToOne(filelist,"D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10_copy.iso");
//		splitFile("D:\\Libraries\\Documents\\我的工具\\Windows_OS\\windows_10.iso",1024*1024*1024);
		batchRename("D:\\Libraries\\Videos\\学习计划", "Day");
//		copyFolder(new File("D:\\Libraries\\Videos\\学习计划\\day21\\code\\demos"),new File("D:\\Libraries"));
//		int index = new Random().nextInt(100);
//		System.out.println(index);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值