📄 dividecake.java
字号:
package DivideCake;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Properties;
import javax.swing.*;
public class DivideCake implements ActionListener{
JFrame jf=new JFrame("分蛋糕");
private final int BASESIZE=1024*1024;
private int blocknu=0;//文件夹分块
private JButton[] jb;// 按钮
private String[] sjb={"打开文件","分割","合并"};
private JTextField filepath = new JTextField(18);// 输入文件框
private JLabel label_file=new JLabel("选择文件");
private JPanel jp1=new JPanel();//对文件的处理对话框架
private JLabel label_size=new JLabel("MB");
private JTextField jt_size=new JTextField("输入文件分割大小",10);
private JPanel jp2=new JPanel();
private JTextArea jta=new JTextArea("说明:分割后会生成.RE文件,合并时找到" +
".RE文件并选定,\n把所有的分割文件放到与.RE同目录下进行合并!");
private String dividepath="";//分割文件的路径
private String filename="";//分割文件的名字
private String directpath="";//直接路径不含有文件名
private int dividesize=1;//默认分为一M
private long filesize=0L;//文件大小
private JMenuBar mb=new JMenuBar();//对菜单进行设置
private JMenu jm=new JMenu("帮助");
private JMenuItem jmi=new JMenuItem("关于");
public DivideCake() {
jf.setSize(380,180);
jf.setLocation(300,250);
jf.setLayout(new GridLayout(3,1));//框架的布局
jb=new JButton[sjb.length];// 按钮
for(int i=0;i<sjb.length;i++){
jb[i]=new JButton(sjb[i]);
jb[i].addActionListener(this);
}
jp1.setLayout(new FlowLayout());//用Panel装入文件的选项
jp1.add(label_file);
jp1.add(filepath);//加入文件选择框
jp1.add(jb[0]);
jf.add(jp1);//把第一个Panel加入到Frame中
jp2.setLayout(new FlowLayout());//分割选项的Panel
jp2.add(jt_size);
jp2.add(label_size);
jp2.add(jb[1]);//加入分割按钮
jp2.add(jb[2]);//加入合并按钮
jf.add(jp2);//加入Panel2
jf.add(jta);
jm.add(jmi);
jmi.addActionListener(this);
mb.add(jm);
jf.setJMenuBar(mb);//加入菜单
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**************文件分割*********************/
public void divideFile(String path) throws IOException{
File f=new File(path);//判断文件是否存在
if("".equals(jt_size.getText())){
JDialog message=new JDialog(jf,"没有输入分割大小",true);
message.add(new JLabel("没有输入分割大小!!"));
message.setSize(150,50);
message.setLocation(300, 250);
message.setVisible(true);
return;
}else{
dividesize=Integer.parseInt(jt_size.getText());//得到分割大小
}
int size=dividesize*BASESIZE;//1M=1024*1024B
if(f.isFile()){
FileInputStream targetfile=new FileInputStream(path);//得到指定文件输入流
FileChannel targetchannel=targetfile.getChannel();//得到输入流文件通道
filesize=targetchannel.size();//得到文件的大小
if((filesize%size)==0){
blocknu=(int)filesize/size;
}else{
blocknu=(int)filesize/size+1;//分割块数
}
Properties p=new Properties();//持久属性集
p.setProperty("FileName", filename);//写入到文件中的文件名
p.setProperty("Block",blocknu+"");
FileOutputStream restore_point=new FileOutputStream(dividepath+".RE");
p.store(restore_point, "还原数据点");//建立还原数据
restore_point.close();
FileOutputStream[] newfile=new FileOutputStream[blocknu];//生成新的文件块
FileChannel[] newfilechannel=new FileChannel[blocknu];//生成新的文件通道
for(int i=0;i<blocknu;i++){
newfile[i]=new FileOutputStream(dividepath+i+"");//生成新的文件
newfilechannel[i]=newfile[i].getChannel();//生成新的文件通道
if(i<blocknu-1){
targetchannel.transferTo(size*i,size, newfilechannel[i]);
}else{
targetchannel.transferTo(size*i,filesize-size*i, newfilechannel[i]);
System.out.println("成功分割");
}//放到对映新文件
}
for(int i=0;i<blocknu;i++){
newfile[i].close();
newfilechannel[i].close();
}
targetfile.close();
targetchannel.close();
}else{
JDialog message=new JDialog(jf,"无此文件存在",true);
message.add(new JLabel("无此文件存在!!"));
message.setSize(150,100);
message.setLocation(300, 250);
message.setVisible(true);
return;
}
String result="成功分割"+filename+"为"+blocknu+"块";
jta.setText(result);
}
/*************文件合并*****************
* @throws IOException */
public void uniteFile(String path) throws IOException{
Properties p=new Properties();//持久属性集
FileInputStream restore_file=new FileInputStream(path);//得到还原文件
p.load(restore_file);
String p_filename=p.getProperty("FileName");
String p_fileblock=p.getProperty("Block");
System.out.println(p_filename+","+p_fileblock);
int p_block=Integer.parseInt(p_fileblock);//得到块数
FileOutputStream fos=
new FileOutputStream((directpath+"/"+p_filename).replace('/', File.separatorChar));
//输出文件
FileChannel fosch=fos.getChannel();
FileInputStream[] fis=new FileInputStream[p_block];//得到要提取的文件
FileChannel[] fch=new FileChannel[p_block];
File[] delf=new File[p_block];//要删除的文件
for(int i=0;i<p_block;i++){
fis[i]=new FileInputStream((directpath+"/"+p_filename+i).replace('/', File.separatorChar));//得到文件
fch[i]=fis[i].getChannel();//得到文件通道
fch[i].transferTo(0, fch[i].size(), fosch);//放入到fosch中
delf[i]=new File((directpath+"/"+p_filename+i).replace('/', File.separatorChar));
}
File delref=new File(path);
for (int i = 0; i < p_block; i++) {
fch[i].close();
fis[i].close();
System.out.println(delf[i].delete());//删除源文件
}
fos.close();
fosch.close();
restore_file.close();
System.out.print(delref.delete());
String result="成功合并"+p_filename+"文件"+"\n并保存到了:"+directpath;
jta.setText(result);
}
/*************监听器******************/
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();//得到按钮的消息
if("打开文件".equals(comm)){
JFileChooser jfc=new JFileChooser();//打开文件夹
jfc.setSize(300,250);
int returnVal=jfc.showOpenDialog(jf);//设置文件夹为打开文件夹
if(returnVal==JFileChooser.APPROVE_OPTION){
filename=jfc.getSelectedFile().getName();//得到文件名
directpath=jfc.getCurrentDirectory().getAbsolutePath();
dividepath=directpath+"/"+filename;
dividepath.replace('/',File.separatorChar);
//为了跨平台更换文件的分隔符
System.out.println(dividepath);//得到文件路径
filepath.setText(dividepath);//显示在文件框中
}
jfc.setVisible(true);
FileInputStream targetfile;
try {
targetfile = new FileInputStream(dividepath);
FileChannel targetchannel=targetfile.getChannel();//得到输入流文件通道
try {
filesize=targetchannel.size();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//得到文件的大小
} catch (FileNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}//得到指定文件输入流
String fileinfor="文件路径:"+dividepath+"\n文件大小"+filesize+"B";
jta.setText(fileinfor);
}else if("分割".equals(comm)){
dividepath=filepath.getText();
try {
divideFile(dividepath);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if("合并".equals(comm)){
try {
uniteFile(filepath.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if("关于".equals(comm)){
JDialog message=new JDialog(jf,"关于",true);
message.setLayout(new GridLayout(4,1));
message.add(new JLabel("欢迎使用DivideCake(1.0)文件分割软件"));
message.add(new JLabel("作者:丁忻浩"));
message.add(new JLabel("时间:2008.1.24"));
message.add(new JLabel("每一次小的成功都要珍惜!"));
message.setSize(270,120);
message.setLocation(300, 250);
message.setVisible(true);
}
}
/************MAIN*****************/
public static void main(String[] args) {
new DivideCake();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -