这两天又把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 bin = new ArrayList();
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 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 list = new ArrayList();
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 filelist,String savepath){
Vector vector = new Vector();
try {
for(String str :filelist){
if(new File(str).exists()){
vector.add(new FileInputStream(str));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Enumeration 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 filelist = new ArrayList();
// 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);
}
}