您的位置:首页 > 编程语言 > Java开发

java文件切割和恢复

2016-07-20 11:56 477 查看
之前做的一个小玩意,功能是切割大文件,分成一个个小文件,并且只有通过生成的配置文件才能恢复过来,说起来还可以通过切割来隐藏一些文件 #坏笑脸

先看下效果:





上代码:

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

public class FileSplit {

public static void main(String[] args) throws Exception {
new MyFrame();

}

}

class MyFrame {

Frame frame;
String openPath,savePath,myDir,openDir;
TextField textField1,textField2,textField3;
InputStream fileReader;
OutputStream fileWriter;
Button openFile,saveFile,splitButton,restoreButton;
int num=1;

public MyFrame() throws Exception {
frame=new Frame("文件切割");
frame.setSize(570, 360);
frame.setLayout(null);
frame.setLocation(300,100);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setResizable(false);
frame.setVisible(true);

//控件
MyCompanies();

//------------------------------------------//
MyEvent();

}

/**
* 绘制窗体
*/
public void MyCompanies() {

Label label1=new Label("打开文件:");
Label label2=new Label("文件存放:");
Label label3=new Label("每份大小:");
Label label4=new Label("MB 1~1000MB");

label1.setBounds(40, 60, 80, 30);
label2.setBounds(40, 100, 80, 30);
label3.setBounds(40, 140, 80, 30);
label4.setBounds(170, 138, 180, 40);

textField1=new TextField();
textField2=new TextField();
textField3=new TextField();

textField1.setBounds(120, 65, 300, 25);
textField2.setBounds(120, 105, 300, 25);
textField3.setBounds(120, 145, 45, 25);

textField1.setFocusable(false);
textField2.setFocusable(false);
// textField3.setFocusable(false);
textField1.setFont(new Font("Monospaced", Font.BOLD, 15));
textField2.setFont(new Font("Monospaced", Font.BOLD, 15));
textField3.setFont(new Font("Monospaced", Font.BOLD, 15));
textField3.setText(num+"");

openFile=new Button("打开..open");
saveFile=new Button("打开..save");
splitButton=new Button("切割");
restoreButton=new Button("合并");

openFile.setFocusable(true);
splitButton.setBounds(140, 180, 100, 100);
restoreButton.setBounds(300, 180, 100, 100);
openFile.setBounds(440, 60, 80, 30);
saveFile.setBounds(440, 100, 80, 30);

frame.add(textField1);
frame.add(textField2);
frame.add(textField3);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(openFile);
frame.add(saveFile);
frame.add(splitButton);
frame.add(restoreButton);

}

/**
* 文件重组恢复
* @throws Exception
*/
public void MyRestore() throws Exception {
if (openPath == null) {

JOptionPane.showMessageDialog(null, "打开一个文件!!");

} else if (savePath == null) {

JOptionPane.showMessageDialog(null, "请选择保存路径!");

} else if (!openPath.endsWith(".properties")) {

JOptionPane.showMessageDialog(null, "配置文件错误!请重新打开配置文件.properties");

}else {

boolean t=true;

ArrayList<FileInputStream> aList=new ArrayList<FileInputStream>();
FileInputStream in = new FileInputStream(openPath);

// 导入Properties
Properties prop = new Properties();
prop.load(in);
int mycount=Integer.parseInt(prop.getProperty("mycount"));

File myFile=new File(openDir);
File[] files=myFile.listFiles(new MyFilter(myFile, ".illusion"));

if (files.length!=mycount) {
JOptionPane.showMessageDialog(null, "文件缺失,合并失败! ");
t=false;

}else {
for (int i = 0; i < mycount; i++) {
aList.add(new FileInputStream(files[i]));
}
}

if (t) {

Iterator<FileInputStream> iterator=aList.iterator();
//枚举使用迭代,匿名内部类
Enumeration<FileInputStream> en=new Enumeration<FileInputStream>() {

@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return iterator.hasNext();
}

@Override
public FileInputStream nextElement() {
// TODO Auto-generated method stub
return iterator.next();
}

};

////////////
SequenceInputStream sis = new SequenceInputStream(en);//整合成一个数据流

FileOutputStream fos=new FileOutputStream(savePath);

byte[] b=new byte[1024];

int len = -1;

while((len=sis.read(b))!=-1) {
fos.write(b,0,len);

}

fos.close();

JOptionPane.showMessageDialog(null, "合并完成!");

}

}
}

/**
* 文件分割保存
* @throws Exception
*/
public void MySplit() throws Exception {
if (openPath==null) {

JOptionPane.showMessageDialog(null, "打开一个文件!!");

}else if(savePath==null){

JOptionPane.showMessageDialog(null, "请选择保存路径!");

}else {
fileReader = new FileInputStream(openPath);
//
// System.out.println("打开路径:"+openPath);
// System.out.println("保存路径:"+savePath);

BufferedInputStream in=new BufferedInputStream(fileReader);
BufferedOutputStream out = null;

int len=-1,count = 1;

num=Integer.parseInt(textField3.getText());

byte[] b=new byte[1024*1024*num];

while ((len=in.read(b))!=-1) {
fileWriter = new FileOutputStream(savePath+"("+(count++)+")"+".illusion");
out=new BufferedOutputStream(fileWriter);
out.write(b,0,len);
out.close();
fileWriter.close();
}

//设置Properties
Properties prop=new Properties();
prop.setProperty("mycount",(count-1)+"");
prop.setProperty("myend", "");
FileOutputStream f=new FileOutputStream(myDir+"illusion.properties");
prop.store(f, "By Humo");

f.close();
in.close();
fileReader.close();

JOptionPane.showMessageDialog(null, "切割完成!");

}

}

public void MyEvent(){
openFile.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
FileDialog aDialog=new FileDialog(frame, "打开..");
aDialog.setVisible(true);
if (aDialog.getDirectory()!=null) {
//获得文件路径
openDir=aDialog.getDirectory();
openPath=(aDialog.getDirectory()+aDialog.getFile());
textField1.setText(openPath);

}

}
});

saveFile.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
FileDialog aDialog=new FileDialog(frame, "打开..",FileDialog.SAVE);
aDialog.setVisible(true);
if (aDialog.getDirectory()!=null) {
myDir=aDialog.getDirectory();
savePath=aDialog.getDirectory()+aDialog.getFile();
textField2.setText(aDialog.getDirectory());

}

}
});

//切割
splitButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
try {
MySplit();
} catch (Exception e1) {e1.printStackTrace();}
}
});

//合并
restoreButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

try {
MyRestore();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
});

}

}

class MyFilter implements FileFilter{

protected String flag=null;
protected File dir=null;
MyFilter(File dir,String flag){
this.flag=flag;
this.dir=dir;
}
@Override
public boolean accept(File dir) {
if (dir.getName().endsWith(flag)) {
System.out.println("找到文件:"+dir.getName());
return true;
}
return false;
}

}


自己将其生成了exe可执行文件,可以直接在电脑上使用    http://download.csdn.net/detail/an_illusion/9581061
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java