分享
 
 
 

使用java.io.*操作文件的拆分与合并,Application一例!

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import javax.swing.text.*;

import javax.swing.event.*;

public class Application1 extends JFrame {

String filepath="";

JLabel splitSourceSize,coalitionTotalSize,coalitionTotalCount;

JTextField SplitSource,SplitTotalCount,SplitEachSize,coalitionSource;

//*****************************************申明结束

public Application1() {

super("工具");

this.setSize(640,480);

this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);

JPanel contentPanel=(JPanel)this.getContentPane();

contentPanel.setLayout(new BorderLayout());

JTabbedPane jtp=new JTabbedPane();

jtp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

JPanel splitPane=new JPanel(new GridLayout(5,1));

JPanel splitPane0=new JPanel(new BorderLayout());

splitPane0.add(splitPane,BorderLayout.NORTH);

JPanel splitPane1=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel splitPane2=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel splitPane3=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel splitPane4=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel splitPane5=new JPanel();

splitPane1.add(new JLabel("拆分源文件:"));

SplitSource=new JTextField(40);

splitPane1.add(SplitSource);

JButton SplitJButton=new JButton("浏览...");

SplitJButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

SplitJButtonClick(e);//选择要拆分的文件

}

});

splitPane1.add(SplitJButton);

splitSourceSize=new JLabel("源文件大小:");

splitPane2.add(splitSourceSize);

splitPane3.add(new JLabel("拆分的数目:"));

SplitTotalCount=new JTextField(15);

SplitTotalCount.setDocument(new NumberDocument());//限制输入框只能输入数字

SplitTotalCount.getDocument().addDocumentListener(new DocumentListener() {//给输入框添加事件

public void changedUpdate(DocumentEvent e) {

if(!Application1.this.SplitTotalCount.isFocusOwner())return;

try{SplitTotalCount_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

public void insertUpdate(DocumentEvent e) {

if(!Application1.this.SplitTotalCount.isFocusOwner())return;

try{SplitTotalCount_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

public void removeUpdate(DocumentEvent e) {

if(!Application1.this.SplitTotalCount.isFocusOwner())return;

try{SplitTotalCount_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

});

splitPane3.add(SplitTotalCount);

splitPane4.add(new JLabel("单文件大小:"));

SplitEachSize=new JTextField(15);

SplitEachSize.setDocument(new NumberDocument());//限制输入框只能输入数字

SplitEachSize.getDocument().addDocumentListener(new DocumentListener() {//给输入框添加事件

public void changedUpdate(DocumentEvent e) {

if(!Application1.this.SplitEachSize.isFocusOwner())return;

try{SplitEachSize_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

public void insertUpdate(DocumentEvent e) {

if(!Application1.this.SplitEachSize.isFocusOwner())return;

try{SplitEachSize_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

public void removeUpdate(DocumentEvent e) {

if(!Application1.this.SplitEachSize.isFocusOwner())return;

try{SplitEachSize_changedUpdate(e);}catch(Exception ex){System.err.println(ex.toString());}

}

});

splitPane4.add(SplitEachSize);

JButton SplitJB=new JButton("开始拆分");

SplitJB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try{SplitJBClick(e);}catch(Exception ex){System.err.println(ex.toString());}//拆分文件

}

});

splitPane5.add(SplitJB);

splitPane.add(splitPane1);

splitPane.add(splitPane2);

splitPane.add(splitPane3);

splitPane.add(splitPane4);

splitPane.add(splitPane5);

JPanel coalitionPane0=new JPanel(new BorderLayout());

JPanel coalitionPane=new JPanel(new GridLayout(4,1));

coalitionPane0.add(coalitionPane,BorderLayout.NORTH);

JPanel coalitionPane1=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel coalitionPane2=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel coalitionPane3=new JPanel(new FlowLayout(FlowLayout.LEADING));

JPanel coalitionPane4=new JPanel();

coalitionPane1.add(new JLabel("合并首文件:"));

coalitionSource=new JTextField(40);

coalitionPane1.add(coalitionSource);

JButton coalitionJButton=new JButton("浏览(*.000)");

coalitionJButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

coalitionJButtonClick(e);//选择要合并的首文件

}

});

coalitionPane1.add(coalitionJButton);

coalitionPane.add(coalitionPane1);

coalitionTotalSize=new JLabel("文件总大小:");

coalitionPane2.add(coalitionTotalSize);

coalitionPane.add(coalitionPane2);

coalitionTotalCount=new JLabel("文件总数目:");

coalitionPane3.add(coalitionTotalCount);

coalitionPane.add(coalitionPane3);

JButton coalitionJB=new JButton("开始合并");

coalitionJB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try{coalitionJBClick(e);}catch(Exception ex){System.err.println(ex.toString());}//合并文件

}

});

coalitionPane4.add(coalitionJB);

coalitionPane.add(coalitionPane4);

jtp.add("拆分文件",splitPane0);

jtp.add("合并文件",coalitionPane0);

contentPanel.add(jtp,BorderLayout.CENTER);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);//当关闭窗口时退出系统

}

});

this.setVisible(true);

//********************************************以上为图形界面设计

}

private void SplitJBClick(ActionEvent e) throws Exception {//拆分文件操作

File file=new File(this.SplitSource.getText());

if(!file.exists()) {

JOptionPane.showMessageDialog(this,"源文件不存在!","警告",JOptionPane.WARNING_MESSAGE);

return;

}

javax.swing.JFileChooser fjc;

fjc=new javax.swing.JFileChooser(filepath);

fjc.removeChoosableFileFilter(fjc.getAcceptAllFileFilter());

fjc.setDialogTitle("选择保存目录");

fjc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if(fjc.APPROVE_OPTION!=fjc.showOpenDialog(this))return;

filepath=fjc.getSelectedFile().getPath();

byte[] b=new byte[1024];

int read=0;

String splitfilename=file.getName();

int eachfilesize=Integer.parseInt(this.SplitEachSize.getText());

FileInputStream fis=new FileInputStream(file);

FileOutputStream fos;

String savesplitfilename="";

for(long splitcount=0;splitcount<Long.parseLong(this.SplitTotalCount.getText());splitcount++) {

savesplitfilename=fjc.getSelectedFile()+"/"+splitfilename+".xiruo.";

if(String.valueOf(splitcount).length()==1)

savesplitfilename+="00"+splitcount;

else if(String.valueOf(splitcount).length()==2)

savesplitfilename+="0"+splitcount;

else

savesplitfilename+=splitcount;

fos=new FileOutputStream(savesplitfilename);

int eachread=b.length;

int hasread=0;

while((read=fis.read(b,0,eachread))>0) {

fos.write(b,0,read);

fos.flush();

hasread+=read;

if(hasread>=eachfilesize&&splitcount!=Long.parseLong(this.SplitTotalCount.getText())-1)

break;

if(eachfilesize-hasread<(long)b.length)

eachread=eachfilesize-hasread;

}

fos.close();

}

fos=null;

JOptionPane.showMessageDialog(this,"拆分完成");

}

private void coalitionJBClick(ActionEvent e) throws Exception {//合并文件操作

File file=new File(this.coalitionSource.getText());

if(!file.exists()) {

JOptionPane.showMessageDialog(this,"源文件不存在!","警告",JOptionPane.WARNING_MESSAGE);

return;

}

javax.swing.JFileChooser fjc;

fjc=new javax.swing.JFileChooser(filepath);

fjc.setDialogTitle("选择保存目录");

fjc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

if(fjc.APPROVE_OPTION!=fjc.showOpenDialog(this))return;

filepath=fjc.getSelectedFile().getPath();

String selfilename=file.getName();

selfilename=selfilename.substring(0,selfilename.lastIndexOf("."));

String savefile=fjc.getSelectedFile().getAbsolutePath()+"/"+selfilename.substring(0,selfilename.lastIndexOf("."));

if(new File(savefile).exists()) {

int javaok=JOptionPane.showConfirmDialog(this,"文件名 "+new File(savefile).getName()+" 的文件已经存在,你要覆盖原来的文件吗?","警告!",JOptionPane.OK_CANCEL_OPTION);

if(javaok==JOptionPane.CANCEL_OPTION)return;

}

byte[] b=new byte[1024];

int read=0;

FileOutputStream fos=new FileOutputStream(savefile);

FileInputStream fis;

File[] f=file.getParentFile().listFiles();

for(int i=0;i<f.length;i++) {

if(!f[i].getName().startsWith(selfilename))continue;

fis=new FileInputStream(f[i]);

while((read=fis.read(b,0,b.length))>0) {

fos.write(b, 0, read);

fos.flush();

}

fis.close();

}

fos.close();

fos=null;

fis=null;

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

}

private void SplitTotalCount_changedUpdate(DocumentEvent e) throws Exception {

File file=new File(this.SplitSource.getText());

if(!file.exists()||this.SplitTotalCount.getText().equals(""))return;

long l=Long.parseLong(this.SplitTotalCount.getText());

if(l<=0)return;

this.SplitEachSize.setText(String.valueOf(file.length()/l));

}

private void SplitEachSize_changedUpdate(DocumentEvent e) throws Exception {

File file=new File(this.SplitSource.getText());

if(!file.exists()||this.SplitEachSize.getText().equals(""))return;

double l=Double.parseDouble(this.SplitEachSize.getText());

if(l<=0)return;

this.SplitTotalCount.setText(fileCount((double)file.length(),l));

}

private void SplitJButtonClick(ActionEvent e) {//选择要拆分的源文件

javax.swing.JFileChooser fjc;

fjc=new javax.swing.JFileChooser(filepath);

fjc.addChoosableFileFilter(new myFilter("*.jpg,*.gif","jpg,gif Files(*.jpg,*.gif)"));//文件过滤

fjc.addChoosableFileFilter(new myFilter("*.rm,*.rmvb","real Files(*.rm,*.rmvb)"));//文件过滤

fjc.addChoosableFileFilter(new myFilter("*.mpg,*.mpeg","mpg Files(*.mpg,*.mpeg)"));//文件过滤

fjc.addChoosableFileFilter(fjc.getAcceptAllFileFilter());

if(fjc.APPROVE_OPTION!=fjc.showOpenDialog(this))return;

filepath=fjc.getSelectedFile().getPath();

File file=fjc.getSelectedFile();

double filesize=file.length();

String size="";

java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");

if(filesize>=1024d*1024d*0.8d)

size=df.format(filesize/(1024d*1024d))+"MB";

else if(filesize>=1024d*0.8d)

size=df.format(filesize/1024d)+"KB";

else

size=filesize+"Bytes";

this.splitSourceSize.setText("源文件大小: "+size);

this.SplitSource.setText(file.getAbsolutePath());

this.SplitTotalCount.setText(fileCount(filesize,102400));

this.SplitEachSize.setText("102400");

}

private void coalitionJButtonClick(ActionEvent e) {//选择要合并的首文件

javax.swing.JFileChooser fjc;

fjc=new javax.swing.JFileChooser(filepath);

fjc.removeChoosableFileFilter(fjc.getAcceptAllFileFilter());//移除默认的select All

fjc.addChoosableFileFilter(new myFilter("*.xiruo.000","拆分首文件(*.xiruo.000)"));//文件过滤

if(fjc.APPROVE_OPTION!=fjc.showOpenDialog(this))return;

filepath=fjc.getSelectedFile().getPath();

File file=fjc.getSelectedFile();

String selfilename=file.getName();

this.coalitionSource.setText(file.getAbsolutePath());

selfilename=selfilename.substring(0,selfilename.lastIndexOf("."));

File[] f=file.getParentFile().listFiles();

double filesize=0;

int filetotalcount= 0;

for(int i=0;i<f.length;i++) {

if(!f[i].getName().startsWith(selfilename))continue;

filetotalcount++;

filesize+=f[i].length();

}

String size="";

java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");

if(filesize>=1024d*1024d*0.8d)

size=df.format(filesize/(1024d*1024d))+"MB";

else if(filesize>=1024d*0.8d)

size=df.format(filesize/1024d)+"KB";

else

size=filesize+"Bytes";

this.coalitionTotalSize.setText("文件总大小: "+size);

this.coalitionTotalCount.setText("文件总数目: "+filetotalcount);

}

private String fileCount(double filesize,double eachsize) {

int i=(int)(filesize/eachsize)+(filesize%eachsize>0?1:0);

return String.valueOf(i);

}

public class NumberDocument extends PlainDocument {//对文本框的输入进行输入限制,构造PlainDocument实现

public void insertString(int offs, String str, AttributeSet a)

throws BadLocationException {

char[] source = str.toCharArray();

char[] result = new char[source.length];

int j = 0;

for (int i = 0; i < result.length; i++) {

if (Character.isDigit(source[i]))

result[j++] = source[i];

}

super.insertString(offs, new String(result, 0, j), a);

}

}

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

Font font=new Font("宋体",Font.PLAIN,15);

UIManager.put("Button.font",font);

UIManager.put("ToggleButton.font",font);

UIManager.put("RadioButton.font",font);

UIManager.put("CheckBox.font",font);

UIManager.put("ColorChooser.font",font);

UIManager.put("ToggleButton.font",font);

UIManager.put("ComboBox.font",font);

UIManager.put("ComboBoxItem.font",font);

UIManager.put("InternalFrame.titleFont",font);

UIManager.put("Label.font",font);

UIManager.put("List.font",font);

UIManager.put("MenuBar.font",font);

UIManager.put("Menu.font",font);

UIManager.put("MenuItem.font",font);

UIManager.put("RadioButtonMenuItem.font",font);

UIManager.put("CheckBoxMenuItem.font",font);

UIManager.put("PopupMenu.font",font);

UIManager.put("OptionPane.font",font);

UIManager.put("Panel.font",font);

UIManager.put("ProgressBar.font",font);

UIManager.put("ScrollPane.font",font);

UIManager.put("Viewport",font);

UIManager.put("TabbedPane.font",font);

UIManager.put("TableHeader.font",font);

UIManager.put("TextField.font",font);

UIManager.put("PasswordFiled.font",font);

UIManager.put("TextArea.font",font);

UIManager.put("TextPane.font",font);

UIManager.put("EditorPane.font",font);

UIManager.put("TitledBorder.font",font);

UIManager.put("ToolBar.font",font);

UIManager.put("ToolTip.font",font);

UIManager.put("Tree.font",font);//以上设置是为了解决中文问题

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//设置可视风格

new Application1();

}

}

//************************************************文件选择过滤器

class myFilter extends javax.swing.filechooser.FileFilter {

String extension="",description="";

public myFilter(String extension,String description) {

if(extension!=null)this.extension=extension;

if(description!=null)this.description=description;

}

public String getDescription() {

return this.description;

}

public boolean accept(File file) {

if(file.isDirectory()||extension.equals(""))

return true;

String[] s=extension.replaceAll("[*]","").split("[,]");

for(int i=0;i<s.length;i++) {

if(file.getName().toLowerCase().endsWith(s[i].toLowerCase())) {

return true;

}

}

return false;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有