一个文件分割的程序
2005-06-22 11:36
363 查看
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();
}
}
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();
}
}
相关文章推荐
- 1.编写一个简单的C语言程序:计算输入多个整数的平均值,并将此程序分割成多个小文件。 2.为第1题中的程序编写makefile文件,用make编译后改成返回最小值,再编译,观察有多少文件不需要重新
- 1.编写一个简单的C语言程序:计算输入多个整数的平均值,并将此程序分割成多个小文件。 2.为第1题中的程序编写makefile文件,用make编译后改成返回最小值,再编译,观察有多少文件不需要重新
- 今天写了一个含配置文件的 文件分割 及 合并 的java程序。
- C#WinForm程序引用的Dll文件都会根目录下我想放在一个专门的DLL文件夹下
- VB 实现大文件的分割与恢复,引用 ADODB.Stream 提供一个过程
- 给朋友写了一个从脚本文件逐行加载执行SQL语句的程序,现在分享给将来需要的朋友们参考
- 自己写的文件分割的程序
- 随便写的一个小程序:将有道阅读导出的rss列表转换成Foxmail的rss兼容的导入文件
- 将输入文件按域分割的程序
- 一个microsoft的.exe程序文件的启动过程
- 64位系统 IIS7.0配置.net网站时报错:未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
- 一个宏命令,就可以程序崩溃时生成dump文件
- 一个将文件复制后以时间命名的Python程序
- 多个.Net程序共用同一个config文件的方法
- 现象:起中文的文件名是乱码,通过程序往一个文件中写入中文是乱码。
- 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序
- 一个简单的管理Web站点文件的页面程序(修改版)
- 未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
- python核心编程第九章_文件系统的一个程序
- 一个简单的Linux内核代码整合到一个文件的Java程序