import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
// 切割类
public class Split extends InitFrame{
private static final long serialVersionUID = 1L;
public Split()
{
super("文件切割","file_split.png",440,260);
this.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
initGUI();
this.setVisible(true);
}
public void initGUI() {
// 设置布局
BorderLayout b = new BorderLayout();
this.setLayout(b);
this.setResizable(false);
// 创建面板
northPanel = new JPanel();
centerPanel = new JPanel();
// 将面板添加到窗体
this.add(northPanel,BorderLayout.NORTH);
this.add(centerPanel,BorderLayout.CENTER);
// 设置面板布局
northPanel.setLayout(new GridBagLayout());
centerPanel.setLayout(null);
// 设置北部面板
radio_M = new JRadioButton("M(兆)",true);
radio_M.setActionCommand("M");
radio_K = new JRadioButton("K(千字节)");
radio_K.setActionCommand("K");
bg = new ButtonGroup();
bg.add(radio_M);
bg.add(radio_K);
field_size = new JTextField(4);
// 添加焦点监听器当失去焦点时候 获取输入的大小
field_size.addActionListener( listener);
northPanel.add(field_size,new GBC(0,0));
northPanel.add(radio_M,new GBC(1,0));
northPanel.add(radio_K,new GBC(2,0));
Border border = BorderFactory.createTitledBorder("请输入分隔的大小");
northPanel.setBorder(border);
// 设置中部面板
label_1 = new JLabel("请选择源文件:");
centerPanel.add(label_1);
label_1.setBounds(20,10 , 120, 30);
label_2 = new JLabel("请选择目标文件夹:");
centerPanel.add(label_2);
label_2.setBounds(20, 50, 150, 30);
//用来显示源文件地址(要切割的文件)
field_1 = new JTextField(40);
centerPanel.add(field_1);
field_1.addActionListener(listener);
field_1.setBounds(130, 10, 200, 30);
//用来显示目标文件夹地址(切割后存放发地点)
field_2 = new JTextField(40);
centerPanel.add(field_2);
field_2.addActionListener(listener);
field_2.setBounds(130, 50,200, 30);
//用来获取源文件(要切割的文件)
button_1= new JButton("选择",new ImageIcon(Split.class.getResource("/resources/open.png")));
centerPanel.add(button_1);
button_1.setBounds(350, 10, 80, 30);
button_1.addActionListener(listener);
//用来获取目标文件夹(切割后存放发地点)
button_2= new JButton("选择",new ImageIcon(Split.class.getResource("/resources/open.png")));
centerPanel.add(button_2);
button_2.addActionListener(listener);
button_2.setBounds(350, 50, 80, 30);
// 下部面板
// 切割文件
button_Split = new JButton("切割",new ImageIcon(Split.class.getResource("/resources/split.png")));
centerPanel.add(button_Split);
button_Split.addActionListener(listener);
button_Split.setBounds(80, 100, 130, 30);
// 打开目标文件夹
button_open = new JButton("打开目标文件夹",new ImageIcon(Split.class.getResource("/resources/open_dir.png")));
centerPanel.add(button_open);
button_open.addActionListener(listener);
button_open.setBounds(250, 100, 140, 30);
}
private class ChoiceListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(button_1))
{
if(radio_M.isSelected())
{
// 当用户没有输入信息便选择文件时,给予提示信息
if(field_size.getText().isEmpty())
{
JLabel label_result = new JLabel("切割文件大小不可为空,请重新输入!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/null.png")));
}else{
size =Integer.parseInt(field_size.getText());//即选择的是 M(兆)
if(size<0||size>1024)
{
JLabel label_result = new JLabel("输入的切割文件大小不合法,请重新输入!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/result.png")));
}
size = 1024*1024*size;//1024 字节乘以1024字节是1M 再乘以size,获取的size得到实际的大小
}
}else if(radio_K.isSelected())
{
// 当用户没有输入信息便选择文件时,给予提示信息
if(field_size.getText().isEmpty())
{
JLabel label_result = new JLabel("切割文件大小不可为空,请重新输入!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/null.png")));
}else{
size =Integer.parseInt(field_size.getText());//即选择的是 M(千字节)
if(size<0||size>1024*1024)
{
JLabel label_result = new JLabel("输入的切割文件大小不合法,请重新输入!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/result.png")));
}
size = 1024*size;//1024 表示千字节再乘以size,获取的size得到实际的大小
}
}
JFileChooser chooser = new JFileChooser();
// 只许选择文件
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 不允许同时选中多个文件
chooser.setMultiSelectionEnabled (false);
int returnVal = chooser.showOpenDialog(Split.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
// 获取选择的文件
file_source= chooser.getSelectedFile();
// 获取的文件名
fileName = file_source.getName();
// 将获取的源文件名显示
field_1.setText(file_source.getAbsolutePath());
}
}else if(e.getSource().equals(button_2))
{
// 如果输入的文件大小大于源文件,要求重新输入
if(file_source.length()<size)
{
showWarningInfo();
}
JFileChooser chooser = new JFileChooser();
// 只许选择文件夹
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// 不允许同时选中多个文件夹
chooser.setMultiSelectionEnabled (false);
int returnVal = chooser.showOpenDialog(Split.this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
// 获取选择的目标文件夹
file_dir= chooser.getSelectedFile();
// 将获取的目标文件名显示
field_2.setText(file_dir.getAbsolutePath());
}
}else if(e.getSource().equals(button_Split))
{
splitFile(file_source);
}else if(e.getSource().equals(button_open))
{
// 获取目标文件目录路径
String file_dir_path = file_dir.getAbsolutePath();
// 打开目标文件目录
Runtime r = Runtime.getRuntime();
try {
r.exec("cmd.exe /c start "+file_dir_path);
} catch (IOException e1) {
// 打开存放切割目录失败是给予提示
JLabel label_result = new JLabel("打开目标目录失败!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-打开目录", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/null.png")));
}
}
}
// 当输入的切割内容大于文件大小时给予警告信息
private void showWarningInfo()
{
JLabel label_result = new JLabel("输入的切割文件大小大于源文件,请重新输入!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/succes.png")));
}
// 根据获取的信息切割文件
private void splitFile(File file_source)
{
try {
FileInputStream fis = new FileInputStream(file_source);
byte [] buf =new byte[size];
FileOutputStream fos =null;
int len = 0;
int count =1;
String dir_path =file_dir.getAbsolutePath();
Properties prop = new Properties();
while((len = fis.read(buf))!=-1)
{
fos = new FileOutputStream(new File(dir_path,(count++)+".split"));
fos.write(buf, 0, len);
fos.close();
}
// 将被切割的文件信息存储到文件信息中
prop.setProperty("splitcount", count+"");
prop.setProperty("filename", fileName);
fos = new FileOutputStream(new File(dir_path,count+".properties"));
prop.store(fos, "saved file information");
// 关闭流
fos.close();
fis.close();
showSuccessInfo();
} catch (FileNotFoundException e) {
showFailedInfo();
} catch (IOException e1) {
showFailedInfo();
}
}
private void showSuccessInfo()
{
JLabel label_result = new JLabel("文件已经成功切割到目标文件夹!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/succes.png")));
}
// 打开切割文件失败是给予提示
private void showFailedInfo()
{
JLabel label_result = new JLabel("打开源文件失败,可能之前切割文件已存在请清理后再切割!!");
JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-打开源文件", JOptionPane.INFORMATION_MESSAGE,JOptionPane.OK_OPTION, new ImageIcon(Split.class.getResource("/resources/result.png")));
}
}
private ChoiceListener listener = new ChoiceListener();
private JPanel northPanel;
private JPanel centerPanel;
private ButtonGroup bg ;
private JRadioButton radio_M;
private JRadioButton radio_K;
private JTextField field_size;
private JLabel label_1;
private JLabel label_2;
private JTextField field_1;
private JTextField field_2;
private JButton button_1;
private JButton button_2;
private JButton button_Split;
private JButton button_open;
private int size=0;//用来获取用户输入的文件分隔大小
private File file_source=null;//用来获取源文件
private File file_dir=null;//用来获取目标文件夹文件
private String fileName="";//用来获取源文件名
}
————————————————
版权声明:本文为CSDN博主「明明如月学长」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w605283073/article/details/46572449