小弟这两天一直在琢磨怎么实现对用户输入的文件进行切割,并能够给用户一定的人性化提示,下面是代码实现:还有不足的地方,忘前辈们多多指点呀!
package fileUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import UI.Main;
/**
* 对文件进行切割
*
* @author jinxiao
*
*/
public class SpiltFile {
public static final int size = 1024*2048;
/**
* 对文件进行切割
* 并将切割的信息存储进配置文件
* @param gofile
* @param buf
* @param toFile
* @throws IOException
*/
public static void spiltFile(File gofile, byte[] buf, File toFile)
throws IOException {
FileInputStream in = new FileInputStream(gofile);
FileOutputStream out = null;
int count = 1;
/*
* 切割文件时,必须要记录住目标文件名,以及文件切割的个数
* 我们采用键值对的形式保存,用到了Properties流
*/
Properties prop = new Properties();
int len = 0;
while ((len = in.read(buf)) != -1) {
out = new FileOutputStream(new File(toFile, "(" + count++ + ")" + ".part"));
out.write(buf, 0, len);
}
/*
* 将配置信息写进文件中
*/
prop.setProperty("filename", gofile.getName());
prop.setProperty("partCount", count+"");
out = new FileOutputStream(new File(toFile,"(" + count++ + ")"+".properties"));
prop.store(out, "sava file info!");
in.close();
out.close();
}
/**
* 对客户输入的文件进行判断,并根据文件大小提示文件切割的数量
*
* @param file 源目录
* @param num 用户输入的文件大小
* @param toFile 目的目录
* @throws IOException
*/
public static void getInfo(File file, int num, File toFile) throws IOException {
byte[] buf = new byte[num * size];
int count = (int) Math.ceil(((file.length()+0.0) / buf.length));
System.out.println("文件将分隔成" + count + "个碎片文件" + "您确定要分隔吗(yes or no)?");
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String line = bufr.readLine();
if ("yes".equalsIgnoreCase(line)) {
//调用文件切割功能
spiltFile(file, buf, toFile);
System.out.println("文件切割成功!");
} else if ("no".equalsIgnoreCase(line)) {
System.out.println("请重新输入");
//记录用户要切割的大小
int number = Main.printNumber(bufr);
getInfo(file, number, toFile);
}
bufr.close();
}
}
package UI;
/**
* 读用户输入的文件进行切割
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import fileUtil.SpiltFile;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("请输入文件目录:格式为:x:\\\\xxx\\\\xxx.xxx");
// 1,获取键盘录入
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
boolean flag = true;
// 2,读取键盘录入的目标,并封装成文件对象
String filename = bufr.readLine();
File file = new File(filename);
// 3,对输入的文件进行循环判断,如果不存在,则提示用户重新输入
while (flag == true) {
if (!file.exists() || !file.isFile()) {
System.out.println("你输入的文件不存在或不是文件!");
System.out.print("请重新输入:");
filename = bufr.readLine();
file = new File(filename);
} else {
// 3,读取客户要存放的位置
System.out.print("请输入要存放的目录:");
String toPath = bufr.readLine();
File toFile = new File(toPath);
if (!toFile.exists()) {
toFile.mkdirs();
}
int num = printNumber(bufr);
//调用切割的方法
SpiltFile.getInfo(file, num, toFile);
flag = false;
}
}
}
/**
* 获取用户录入的数字
* @param bufr
* @return
* @throws IOException
*/
public static int printNumber(BufferedReader bufr) throws IOException {
System.out.print("请您输入分隔文件的大小(数字1-10之间):");
int num = Integer.parseInt(bufr.readLine());
return num;
}
}
文件切割之键盘录入
最新推荐文章于 2022-03-22 09:33:45 发布