java文件的拆分与合并.docx
- 文档编号:4164298
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:9
- 大小:17.86KB
java文件的拆分与合并.docx
《java文件的拆分与合并.docx》由会员分享,可在线阅读,更多相关《java文件的拆分与合并.docx(9页珍藏版)》请在冰豆网上搜索。
java文件的拆分与合并
150%> importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; importjava.util.*; import op=article&action=keyword&keyword=java.io">java.io.*; importjavax.swing.text.*; importjavax.swing.event.*; 150%>publicclassApplication1extendsJFrame{ Stringfilepath=""; JLabelsplitSourceSize,coalitionTotalSize,coalitionTotalCount; JTextFieldSplitSource,SplitTotalCount,SplitEachSize,coalitionSource; //*****************************************申明结束 publicApplication1(){ super("工具"); this.setSize(640,480); this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2); JPanelcontentPanel=(JPanel)this.getContentPane(); contentPanel.setLayout(newBorderLayout()); JTabbedPanejtp=newJTabbedPane(); jtp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); JPanelsplitPane=newJPanel(newGridLayout(5,1)); JPanelsplitPane0=newJPanel(newBorderLayout()); splitPane0.add(splitPane,BorderLayout.NORTH); JPanelsplitPane1=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelsplitPane2=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelsplitPane3=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelsplitPane4=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelsplitPane5=newJPanel(); splitPane1.add(newJLabel("拆分源文件: ")); SplitSource=newJTextField(40); splitPane1.add(SplitSource); JButtonSplitJButton=newJButton("浏览..."); SplitJButton.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ SplitJButtonClick(e);//选择要拆分的文件 } }); splitPane1.add(SplitJButton); splitSourceSize=newJLabel("源文件大小: "); splitPane2.add(splitSourceSize); splitPane3.add(newJLabel("拆分的数目: ")); SplitTotalCount=newJTextField(15); SplitTotalCount.setDocument(newNumberDocument());//限制输入框只能输入数字 SplitTotalCount.getDocument().addDocumentListener(newDocumentListener(){//给输入框添加事件 publicvoidchangedUpdate(DocumentEvente){ if(! Application1.this.SplitTotalCount.isFocusOwner())return; try{SplitTotalCount_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } publicvoidinsertUpdate(DocumentEvente){ if(! Application1.this.SplitTotalCount.isFocusOwner())return; try{SplitTotalCount_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } publicvoidremoveUpdate(DocumentEvente){ if(! Application1.this.SplitTotalCount.isFocusOwner())return; try{SplitTotalCount_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } }); splitPane3.add(SplitTotalCount); splitPane4.add(newJLabel("单文件大小: ")); SplitEachSize=newJTextField(15); SplitEachSize.setDocument(newNumberDocument());//限制输入框只能输入数字 SplitEachSize.getDocument().addDocumentListener(newDocumentListener(){//给输入框添加事件 publicvoidchangedUpdate(DocumentEvente){ if(! Application1.this.SplitEachSize.isFocusOwner())return; try{SplitEachSize_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } publicvoidinsertUpdate(DocumentEvente){ if(! Application1.this.SplitEachSize.isFocusOwner())return; try{SplitEachSize_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } publicvoidremoveUpdate(DocumentEvente){ if(! Application1.this.SplitEachSize.isFocusOwner())return; try{SplitEachSize_changedUpdate(e);}catch(Exceptionex){System.err.println(ex.toString());} } }); splitPane4.add(SplitEachSize); JButtonSplitJB=newJButton("开始拆分"); SplitJB.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ try{SplitJBClick(e);}catch(Exceptionex){System.err.println(ex.toString());}// op=article&action=keyword&keyword=拆分文件">拆分文件 } }); splitPane5.add(SplitJB); splitPane.add(splitPane1); splitPane.add(splitPane2); splitPane.add(splitPane3); splitPane.add(splitPane4); splitPane.add(splitPane5); JPanelcoalitionPane0=newJPanel(newBorderLayout()); JPanelcoalitionPane=newJPanel(newGridLayout(4,1)); coalitionPane0.add(coalitionPane,BorderLayout.NORTH); JPanelcoalitionPane1=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelcoalitionPane2=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelcoalitionPane3=newJPanel(newFlowLayout(FlowLayout.LEADING)); JPanelcoalitionPane4=newJPanel(); coalitionPane1.add(newJLabel("合并首文件: ")); coalitionSource=newJTextField(40); coalitionPane1.add(coalitionSource); JButtoncoalitionJButton=newJButton("浏览(*.000)"); coalitionJButton.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ coalitionJButtonClick(e);//选择要合并的首文件 } }); coalitionPane1.add(coalitionJButton); coalitionPane.add(coalitionPane1); coalitionTotalSize=newJLabel("文件总大小: "); coalitionPane2.add(coalitionTotalSize); coalitionPane.add(coalitionPane2); coalitionTotalCount=newJLabel("文件总数目: "); coalitionPane3.add(coalitionTotalCount); coalitionPane.add(coalitionPane3); JButtoncoalitionJB=newJButton("开始合并"); coalitionJB.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ try{coalitionJBClick(e);}catch(Exceptionex){System.err.println(ex.toString());}// op=article&action=keyword&keyword=合并文件">合并文件 } }); coalitionPane4.add(coalitionJB); coalitionPane.add(coalitionPane4); jtp.add(" op=article&action=keyword&keyword=拆分文件">拆分文件",splitPane0); jtp.add(" op=article&action=keyword&keyword=合并文件">合并文件",coalitionPane0); contentPanel.add(jtp,BorderLayout.CENTER); this.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0);//当关闭窗口时退出系统 } }); this.setVisible(true); //********************************************以上为图形界面设计 } 150%>privatevoidSplitJBClick(ActionEvente)throwsException{// op=article&action=keyword&keyword=拆分文件">拆分文件操作 Filefile=newFile(this.SplitSource.getText()); if(! file.exists()){ JOptionPane.showMessageDialog(this,"源文件不存在! ","警告",JOptionPane.WARNING_MESSAGE); return; } javax.swing.JFileChooserfjc; fjc=newjavax.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=newbyte[1024]; intread=0; Stringsplitfilename=file.getName(); inteachfilesize=Integer.parseInt(this.SplitEachSize.getText()); FileInputStreamfis=newFileInputStream(file); FileOutputStreamfos; Stringsavesplitfilename=""; for(longsplitcount=0;splitcount savesplitfilename=fjc.getSelectedFile()+"/"+splitfilename+".xiruo."; if(String.valueOf(splitcount).length()==1) savesplitfilename+="00"+splitcount; elseif(String.valueOf(splitcount).length()==2) savesplitfilename+="0"+splitcount; else savesplitfilename+=splitcount; fos=newFileOutputStream(savesplitfilename); inteachread=b.length; inthasread=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,"拆分完成"); } 150%>privatevoidcoalitionJBClick(ActionEvente)throwsException{// op=article&action=keyword&keyword=合并文件">合并文件操作 Filefile=newFile(this.coalitionSource.getText()); if(! file.exists()){ JOptionPane.showMessageDialog(this,"源文件不存在! ","警告",JOptionPane.WARNING_MESSAGE); return; } javax.swing.JFileChooserfjc; fjc=newjavax.swing.JFileChooser(filepath); fjc.setDialogTitle("选择保存目录"); fjc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if(fjc.APPROVE_OPTION! =fjc.showOpenDialog(this))return; filepath=fjc.getSelectedFile().getPath(); Stringselfilename=file.getName(); selfilename=selfilename.substring(0,selfilename.lastIndexOf(".")); Stringsavefile=fjc.getSelectedFile().getAbsolutePath()+"/"+selfilename.substring(0,selfilename.lastIndexOf(".")); if(newFile(savefile).exists()){ intjavaok=JOptionPane.showConfirmDialog(this,"文件名"+newFile(savefile).getName()+"的文件已经存在,你要覆盖原来的文件吗? ","警告! ",JOptionPane.OK_CANCEL_OPTION); if(javaok==JOptionPane.CANCEL_OPTION)return; } byte[]b=newbyte[1024]; intread=0; FileOutputStreamfos=newFileOutputStream(savefile); FileInputStreamfis; File[]f=file.getParentFile().listFiles(); for(inti=0;i if(! f[i].getName().startsWith(selfilename))continue; fis=newFileInputStream(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,"合并完成"); } 150%>privatevoidSplitTotalCount_changedUpdate(DocumentEvente)throwsException{ Filefile=newFile(this.SplitSource.getText()); if(! file.exists()||this.SplitTotalCount.getText().equals(""))return; longl=Long.parseLong(this.SplitTotalCount.getText()); if(l<=0)return; this.SplitEachSize.setText(String.valueOf(file.length()/l)); } 150%>privatevoidSplitEachSize_changedUpdate(DocumentEvente)throwsException{ Filefile=newFile(this.SplitSource.getText()); if(! file.exists()||this.SplitEachSize.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 文件 拆分 合并