📄 filepartition.java
字号:
package fileCutter;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.io.*;
import java.text.DecimalFormat;
public class FilePartition extends JFrame implements ActionListener,ChangeListener {
/**
* 这个面板主要是发现了分割和合并面板的共同之处,故而加以了改进之使用一套面板即可完成可两个页面
*/
private static final long serialVersionUID = 1L;
float abssize=0; //分割文件时的子文件大小
int bufsize=1024*1024;//输入输出流缓冲区的大小
boolean noproblem=true;//各项信息都填充正确
File file_in,file_out; //读入和写出的两个文件
JTabbedPane tabpane;
JPanel[] panel=new JPanel[10];
JButton[] button=new JButton[5];
JLabel labelIn,labelOut,instructionA,instructionB;
JTextField textIn,textOut,size,num;
TextArea info;
JRadioButton bysize,bynum;
JComboBox box;
ImageIcon ico,pic;
ImagePanel imagepanel;
String[] str={"选择文件","选择路径","确定","重置","退出"};//即对应五个按钮的次序
String[] items={"byte","Kb","Mb"};//选择框中的内容
String instruction="1.导入列表文件来合并时,需确保列表文件和子文件在同一目录下\n"+
"\n2.合并时要求子文件不能有缺失,否则会合并失败\n";
FilePartition(){
super("文件分割器");
for(int i=0;i<panel.length;i++)
panel[i]=new JPanel();
for(int i=0;i<button.length;i++){
button[i]=new JButton(str[i]);
}
labelIn=new JLabel("输入文件");
labelOut=new JLabel("输出位置");
instructionA=new JLabel("分割状态:选择待分割文件、目标位置和分割方式,确定即可");
instructionB=new JLabel("合并状态:选择待合并文件的.list文件、目标位置,确定即可");
textIn=new JTextField(null,20);textIn.setEditable(false);
textOut=new JTextField(null,20);textOut.setEditable(false);
size=new JTextField(4);
num=new JTextField(6);num.setEditable(false);
info=new TextArea(5,50);info.setEditable(false);
bysize=new JRadioButton("按大小分割");bysize.setSelected(true);
bynum=new JRadioButton("按个数分割");
box=new JComboBox();
for(int i=0;i<items.length;i++)
box.addItem(items[i]);
box.setSelectedIndex(1);
ico=new ImageIcon(this.getClass().getResource("owner.ico"));
imagepanel=new ImagePanel(ico);
pic=new ImageIcon(this.getClass().getResource("System Finder.pic"));
tabpane=new JTabbedPane(); //㈠选项卡模块
tabpane.setTabPlacement(JTabbedPane.TOP);
tabpane.addTab("分割文件", panel[6]);panel[6].add(instructionA);
tabpane.addTab("合并文件",panel[7]);panel[7].add(instructionB);
add(tabpane,BorderLayout.NORTH); //面板主要由三部分构成:㈠.选项卡tabpane
add(panel[0],BorderLayout.CENTER); //㈡.panel[0]中的三大组件
add(panel[5],BorderLayout.SOUTH); //㈢.最下方的执行按钮㈠㈡㈢㈣㈤
panel[0].add(panel[2],BorderLayout.NORTH);//㈡主面板panel[0]的组成:①
panel[0].add(panel[3],BorderLayout.CENTER); //②
panel[0].add(panel[4],BorderLayout.SOUTH); //③
panel[2].setBorder(new TitledBorder("路径选择")); //①路径选择模块panel[2]
panel[2].setLayout(new GridLayout(2,1));
JPanel p1,p2;p1=new JPanel();p2=new JPanel();
panel[2].add(p1);panel[2].add(p2);
p1.add(labelIn);p1.add(textIn);p1.add(button[0]);
p2.add(labelOut);p2.add(textOut);p2.add(button[1]);
panel[3].setBorder(new TitledBorder("文件信息")); //②文件信息模块panel[3]
panel[3].add(info);
panel[4].setBorder(new TitledBorder("选择分割方式"));//③文件分割方式选择模块panel[4]
ButtonGroup group=new ButtonGroup();group.add(bysize);group.add(bynum);
panel[4].add(bysize);panel[4].add(size);panel[4].add(box);panel[4].add(bynum);panel[4].add(num);
panel[5].add(button[2]);panel[5].add(button[3]);panel[5].add(button[4]);//㈢执行按钮模块panel[5]
///*以下为事件注册监听(含部分处理)模块*///
tabpane.addChangeListener(this);
for(int i=2;i<button.length;i++)
button[i].addActionListener(this);
button[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JFileChooser choose=new JFileChooser();
if(tabpane.getSelectedComponent().equals(panel[7]))
choose.setFileFilter(new MyFileFilter("list","分割时产生的列表文件.list"));
int returnVal=choose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{ file_in=choose.getSelectedFile();
if(tabpane.getSelectedComponent().equals(panel[7])&&!file_in.getPath().endsWith("list")){
JOptionPane.showMessageDialog(null,"请选择.list文件","错误",JOptionPane.WARNING_MESSAGE);
clear();info.setText(instruction);
}
textIn.setText(file_in.getAbsolutePath());
info.setText("文件名:"+file_in.getName()+'\n'+'\n'+"路径:"+file_in.getPath()+'\n'+'\n');
DecimalFormat dF = new DecimalFormat("0.00");
if(tabpane.getSelectedComponent().equals(panel[6]))
info.setText(info.getText()+"大小:"+file_in.length()+"byte = "+dF.format(file_in.length()/1024.0)
+"Kb = "+dF.format(file_in.length()/1024/1024.0)+"Mb");
}
}});
button[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JFileChooser choose=new JFileChooser();
choose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal=choose.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION)
{ file_out=choose.getSelectedFile();
textOut.setText(file_out.getAbsolutePath());
if(tabpane.getSelectedComponent().equals(panel[7]))
info.setText(info.getText()+"输出位置:"+file_out);
}
}});
bysize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bysize.isSelected()){
size.setEditable(true);
num.setEditable(false);
num.setText(null);
}}});
bynum.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bynum.isSelected()){
num.setEditable(true);
size.setEditable(false);
size.setText(null);
}}});
imagepanel.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
try {Runtime.getRuntime().exec("explorer http://se.xjtu.edu.cn");
}catch (IOException e) {}
}});
pack();
setVisible(true);
setBounds(300,120,405,450);
setResizable(false);
setIconImage(pic.getImage());
} //End of NewFrame()
public void actionPerformed(ActionEvent ae) {
String str=ae.getActionCommand();
if(bynum.isSelected()){
num.setEditable(true);
size.setEditable(false);
}
if(bysize.isSelected()){
size.setEditable(true);
num.setEditable(false);
}
if(str.equals("确定")) {
if(file_in==null||file_out==null) //没有选择文件或者位置时报错
JOptionPane.showMessageDialog(null,"请将信息填写完整","请注意",JOptionPane.WARNING_MESSAGE);
else{
if(tabpane.getSelectedComponent().equals(panel[6])){
if(bysize.isSelected()){
try{abssize=Float.parseFloat(size.getText());
if(!(abssize>0))throw new NumberFormatException();
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null,"输入的分割大小有误,请输入合理的大小","请注意",JOptionPane.WARNING_MESSAGE);
size.setText(null);noproblem=false;
}
switch(box.getSelectedIndex()){
case 0:break;
case 1:abssize=Math.round(abssize*1024);break;
case 2:abssize=Math.round(abssize*1024*1024);break;
}
}
else if(bynum.isSelected()){
int numInt=3;
try{numInt=Integer.parseInt(num.getText());
if(!(numInt>0))throw new NumberFormatException();
}catch(NumberFormatException nfe4){
JOptionPane.showMessageDialog(null,"输入的分割个数有误,请输入合理的个数","请注意",JOptionPane.WARNING_MESSAGE);
num.setText(null);noproblem=false;
}
abssize=file_in.length()/numInt+file_in.length()%numInt;
}
if(noproblem)split(abssize);
}
else if(noproblem)combine(file_in.getAbsolutePath());
}
noproblem=true;
}
if(str.equals("重置")) clear();
if(str.equals("退出")) System.exit(0);
}
private void split(float size){ //分割方法
try{
info.setText(info.getText().concat("\n^_^ 分割中→ → ^_^+^_^"));
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file_in),bufsize);
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(new File(file_out,"1_"+file_in.getName())),bufsize);
BufferedWriter list=new BufferedWriter(new FileWriter(new File(file_out,"list_of_"+file_in.getName()+".list")));
int n = 1;
list.write(file_in.getName()+'\n'); //list文件中包括了原文件名和子文件名
for (int b, i = 0; (b = in.read()) != -1; i++) {
if (i>=size) {
out.close();
list.write(n+"_"+file_in.getName()+'\n');
out=new BufferedOutputStream(new FileOutputStream(new File(file_out,++n+"_"+file_in.getName())),bufsize);
i=0;
}
out.write(b);
}
out.close();
list.write(n+"_"+file_in.getName()+'\n');
list.close();
JOptionPane.showMessageDialog(null, "分割操作已完成\n子文件和.list文件都不要弄丢哦!");
}
catch(FileNotFoundException fnfe){JOptionPane.showMessageDialog(null,fnfe,"错误",JOptionPane.WARNING_MESSAGE);}
catch(EOFException eof) {JOptionPane.showMessageDialog(null,eof,"错误",JOptionPane.WARNING_MESSAGE);}
catch(IOException ioe) {JOptionPane.showMessageDialog(null,ioe,"错误",JOptionPane.WARNING_MESSAGE);}
}
private void combine(String file){//合并方法
try{
info.setText(info.getText().concat("\n^_^+^_^ 合并中→ → ^_^"));
String str="";
String inPath="";
BufferedOutputStream out;
BufferedInputStream in;
BufferedReader read=new BufferedReader(new FileReader(file)); //从.list中读取文件名
File exist=new File(file_out,str=read.readLine());
if(exist.exists()) out=new BufferedOutputStream(new FileOutputStream(new File(file_out,"$"+str)),bufsize);
else out=new BufferedOutputStream(new FileOutputStream(new File(file_out,str)),bufsize);
inPath=file_in.getParent();
str=read.readLine();
while(str!=null){
in=new BufferedInputStream(new FileInputStream(new File(inPath,str)),bufsize);
for(int b;(b=in.read())!=-1;){
out.write(b);
}
in.close();
str=read.readLine();
}
out.close();
read.close();
JOptionPane.showMessageDialog(null, "合并操作已完成\n文件在"+file_out);
///*下面的代码用于合并成功以后删除分割时产生的子文件*///
BufferedReader delete=new BufferedReader(new FileReader(file));
String tempStr=delete.readLine();
while((tempStr=delete.readLine())!=null){
File killFile=new File(file_in.getParent(),tempStr);
killFile.delete();
}
delete.close();
File killlistFile=new File(file);
killlistFile.delete();
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(null,fnfe+"\n合并失败,请确认.list文件和子文件没有缺失后重新操作","错误",JOptionPane.WARNING_MESSAGE);
clear();
}
catch(EOFException eof) {
JOptionPane.showMessageDialog(null,eof+"文件未写入结束信息,无法完成操作","错误",JOptionPane.WARNING_MESSAGE);
}
catch(IOException ioe) {
JOptionPane.showMessageDialog(null,ioe+"数据流读写过程发生未知错误","错误",JOptionPane.WARNING_MESSAGE);
}
catch(SecurityException se) {
JOptionPane.showMessageDialog(null,se+"删除文件过程发生未知错误\n可能文件正在使用或被保护或已不存在","错误",JOptionPane.WARNING_MESSAGE);
}
}
public void stateChanged(ChangeEvent arg0) {
clear(); //如果选项卡发生了变化,若切换到分割界面则显示panel[4](选择分割方式模块),否则显示图片;其他区域只改变名称
if(tabpane.getSelectedComponent().equals(panel[6])){
panel[2].setBorder(new TitledBorder("路径选择"));
panel[3].setBorder(new TitledBorder("文件信息"));
info.setText("");
panel[0].remove(imagepanel);
panel[0].add(panel[4]);
imagepanel.setVisible(false);
panel[4].setVisible(true);
}
else if(tabpane.getSelectedComponent().equals(panel[7])){
panel[2].setBorder(new TitledBorder("导入列表文件"));
panel[3].setBorder(new TitledBorder("注意事项"));
info.setText(instruction);
panel[0].remove(panel[4]);
panel[0].add(imagepanel);
panel[4].setVisible(false);
imagepanel.setVisible(true);
}
}
private void clear() { //重置和页面切换时的清理工作由改方法完成
textIn.setText("");textOut.setText("");size.setText("");num.setText("");
if(tabpane.getSelectedComponent().equals(panel[6]))info.setText("");
file_in=file_out=null;
}
public static void main(String args[]){
FilePartition a=new FilePartition();
a.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
} //End of main function
}//End of Jiemian class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -