自己写的文件分割的程序
2005-06-21 14:07
232 查看
终于买了个闪存,128M的,中关村买的,花了120嘿嘿,
tmd,最近真穷,考虑到遇到大文件的问题,到网上下了个文件分割的软件,竟然要注册,什么玩艺啊,算了,还是自己写的好用
先发上了,谁看了有什么意见可以提提,本人比较懒,就没写文件合成的,只是生成了一个批处理,unix下是不能用这个批处理了,呵呵
import java.io.*;
import java.util.*;
public class FileSplit{
protected int size; //size of every sub file
protected String fName; //
protected Vector vNames = new Vector();
protected static final String BatName = "resume.bat";
public FileSplit(String fName){
this.fName = fName;
this.size = 1024*1024; //default size 1M
}
public FileSplit(String fName, int size){
this.fName = fName;
this.size = size*1024*1024;
}
public File getFile(){
File f = new File(fName);
if(!f.exists()){
f = null;
}
return f;
}
public String getName(){
return fName;
}
public int getSize(){
return size;
}
public void splitFile() throws Exception{
System.out.println("Spliting files...");
FileInputStream in = new FileInputStream(getFile());
byte[] buf = new byte[getSize()];
int i = 0;
int ret;
while((ret = in.read(buf)) != -1){
//System.out.println(ret + " bytes read");
save(fName+new Integer(i).toString(), buf, ret);
i++;
}
}
public void createBatFile(){
String fName = "";
if(getFile().getParent() != null){
fName = getFile().getParent();
}
fName += BatName;
String files = "";
for(Enumeration e = vNames.elements(); e.hasMoreElements(); ){
files += (String)e.nextElement();
if(e.hasMoreElements()){
files += " + ";
}
}
try{
FileOutputStream out = new FileOutputStream(new File(fName));
out.write("echo off/r/n".getBytes());
out.write("copy /B ".getBytes());
out.write(files.getBytes());
out.write((" "+getName()).getBytes());
out.write("/r/n".getBytes());
out.write("del ".getBytes());
out.write(files.getBytes());
out.write("/r/n".getBytes());
out.write(("del " + BatName).getBytes());
out.flush();
out.close();
}catch(Exception ex){
System.err.println("Unable to create bat file!");
}
}
private void addFileName(String fName){
vNames.addElement(fName);
}
private Vector getNames(){
return vNames;
}
private void save(String fName, byte[] buf, int len) throws Exception{
File f = new File(fName);
FileOutputStream out =new FileOutputStream(f);
out.write(buf, 0, len);
out.flush();
out.close();
addFileName(f.getName());
System.out.println("File " + fName + " written.");
}
public static void main(String[] args){
FileSplit split = null;
if(args.length == 1){
split = new FileSplit(args[0]);
}
else if(args.length == 2){
split = new FileSplit(args[0], Integer.parseInt(args[1]));
}
else{
System.err.println("need parameters: filename size(must be larger than 1M,default 1M)");
System.exit(1);
}
try{
split.splitFile();
}
catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
split.createBatFile();
}
}
tmd,最近真穷,考虑到遇到大文件的问题,到网上下了个文件分割的软件,竟然要注册,什么玩艺啊,算了,还是自己写的好用
先发上了,谁看了有什么意见可以提提,本人比较懒,就没写文件合成的,只是生成了一个批处理,unix下是不能用这个批处理了,呵呵
import java.io.*;
import java.util.*;
public class FileSplit{
protected int size; //size of every sub file
protected String fName; //
protected Vector vNames = new Vector();
protected static final String BatName = "resume.bat";
public FileSplit(String fName){
this.fName = fName;
this.size = 1024*1024; //default size 1M
}
public FileSplit(String fName, int size){
this.fName = fName;
this.size = size*1024*1024;
}
public File getFile(){
File f = new File(fName);
if(!f.exists()){
f = null;
}
return f;
}
public String getName(){
return fName;
}
public int getSize(){
return size;
}
public void splitFile() throws Exception{
System.out.println("Spliting files...");
FileInputStream in = new FileInputStream(getFile());
byte[] buf = new byte[getSize()];
int i = 0;
int ret;
while((ret = in.read(buf)) != -1){
//System.out.println(ret + " bytes read");
save(fName+new Integer(i).toString(), buf, ret);
i++;
}
}
public void createBatFile(){
String fName = "";
if(getFile().getParent() != null){
fName = getFile().getParent();
}
fName += BatName;
String files = "";
for(Enumeration e = vNames.elements(); e.hasMoreElements(); ){
files += (String)e.nextElement();
if(e.hasMoreElements()){
files += " + ";
}
}
try{
FileOutputStream out = new FileOutputStream(new File(fName));
out.write("echo off/r/n".getBytes());
out.write("copy /B ".getBytes());
out.write(files.getBytes());
out.write((" "+getName()).getBytes());
out.write("/r/n".getBytes());
out.write("del ".getBytes());
out.write(files.getBytes());
out.write("/r/n".getBytes());
out.write(("del " + BatName).getBytes());
out.flush();
out.close();
}catch(Exception ex){
System.err.println("Unable to create bat file!");
}
}
private void addFileName(String fName){
vNames.addElement(fName);
}
private Vector getNames(){
return vNames;
}
private void save(String fName, byte[] buf, int len) throws Exception{
File f = new File(fName);
FileOutputStream out =new FileOutputStream(f);
out.write(buf, 0, len);
out.flush();
out.close();
addFileName(f.getName());
System.out.println("File " + fName + " written.");
}
public static void main(String[] args){
FileSplit split = null;
if(args.length == 1){
split = new FileSplit(args[0]);
}
else if(args.length == 2){
split = new FileSplit(args[0], Integer.parseInt(args[1]));
}
else{
System.err.println("need parameters: filename size(must be larger than 1M,default 1M)");
System.exit(1);
}
try{
split.splitFile();
}
catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
split.createBatFile();
}
}
相关文章推荐
- 文本文件单词分割程序
- 今天写了一个含配置文件的 文件分割 及 合并 的java程序。
- 文件分割程序
- 自己动手编写启动JAVA程序的EXE文件
- 保存UIImage到文件--到本地自己的程序目录
- 自己编写程序批量合并多个“规范定义电子名片(vCard)”文件
- 如何给自己程序通过命令行添加logging.properties文件
- MFC程序中将软件打包后,setup.exe文件自己电脑运行正常,别人电脑显示找不到文件路径
- 自己制作程序安装文件
- 自己以前用C#写的简单升级程序源码(比如更新一个安装包或者压缩文件),带进度条,支持续传
- java程序调用exe文件和调用自己打的jar包或其他外部jar包
- 点击任意exe文件打开自己程序---SDK实现
- 如何自己编写程序文件清理电脑垃圾
- 自己编写程序批量合并多个“规范定义电子名片(vCard)”文件
- 八、将 程序 分割成 多个文件
- 如何卸载自己手机上的APK文件程序
- 转:关于程序优化的问题 [根据某文件 自己总结下]
- Linux手动分割文件,同时不影响程序的日志输出
- java中split()函数,"."应该为''\\.",注意转义字符! 程序对分割后的文件碎片进行合并。