Java TXT文件UI分割源代码

来源:互联网 发布:平湖哪里有淘宝培训 编辑:程序博客网 时间:2024/06/01 17:09
/*NetBeans6.1 编辑...
 * SpiderEditorUI.java
 *
 * Created on 2008年7月19日, 下午9:10
 */
package my.spidereditorui;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author  dow
 */
public class SpiderEditorUI extends javax.swing.JFrame {

    private String strFilePath;
    private String strOutPutPath;
    private int iFileSize = 15;
    private String fileName = "";

    private String getFileName() {
        return fileName;
    }

    private void setFileName(String Name) {
        fileName = Name;
    }

    private void setFileSize(int numSize) {
        iFileSize = numSize;
    }

    private int getFileSize() {
        return iFileSize;
    }

    private void setOutPutPath(String path) {
        strOutPutPath = path + "//" + getFileName().split("[.]")[0];
    }

    private String getOutPutPath() {
        return strOutPutPath;
    }

    private void setFilePath(String path) {
        strFilePath = path;
    }

    private String getFilePath() {
        return strFilePath;
    }

    /** Creates new form SpiderEditorUI */
    public SpiderEditorUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        BtnSplit = new javax.swing.JButton();
        BtnChoose = new javax.swing.JButton();
        BtnDestination = new javax.swing.JButton();
        labelTxt = new javax.swing.JLabel();
        TxtFileSize = new javax.swing.JTextField();
        TxtInputAddress = new javax.swing.JTextField();
        TxtOutPutAddress = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("分割TXT文件小工具");
        setBackground(new java.awt.Color(204, 255, 204));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);

        BtnSplit.setText("开始分割文件");
        BtnSplit.setActionCommand("");
        BtnSplit.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        BtnSplit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnSplitActionPerformed(evt);
            }
        });

        BtnChoose.setText("选取目标文件");
        BtnChoose.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        BtnChoose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnChooseActionPerformed(evt);
            }
        });

        BtnDestination.setText("设置目标文件夹");
        BtnDestination.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        BtnDestination.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnDestinationActionPerformed(evt);
            }
        });

        labelTxt.setText("设置TXT文件大小(KB)");
        labelTxt.setBorder(new javax.swing.border.MatteBorder(null));

        TxtFileSize.setText("15");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(TxtOutPutAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(TxtInputAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BtnChoose, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                                .addGap(321, 321, 321))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(TxtFileSize, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(BtnSplit)
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(369, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BtnDestination, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(BtnChoose, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TxtInputAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(BtnDestination, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(TxtOutPutAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(labelTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BtnSplit, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TxtFileSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );

        pack();
    }// </editor-fold>                        

private void BtnSplitActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BufferedReader reader = null;
    File dirFile;
    boolean bFile = false;
    dirFile = new File(getOutPutPath());
    bFile = dirFile.exists();

    if (bFile == true) {
        int n = JOptionPane.showConfirmDialog(this, "文件夹已经存在" + getOutPutPath() + " 是否删除原来的文件夹及其内容", "确认对话框", JOptionPane.YES_NO_OPTION);
        return;
    } else {
        bFile = dirFile.mkdir();
        if (bFile != true) {
            JOptionPane.showMessageDialog(this, "文件夹创建失败", "确认对话框", JOptionPane.WARNING_MESSAGE);
        }
    }
    try {
        String sizeInput = TxtFileSize.getText();
        setFileSize(Integer.parseInt(sizeInput));
        int filePoint = 0;
        int MAX_SIZE = getFileSize() * 1024;
        BufferedWriter writer = null;
        reader = new BufferedReader(new FileReader(getFilePath()));
        StringBuffer buffer = new StringBuffer();
        String line = reader.readLine();
        while (line != null) {
            buffer.append(line).append("/r/n");
            if (buffer.toString().getBytes().length >= MAX_SIZE) {
                writer = new BufferedWriter(new FileWriter(getOutPutPath() + "//"  + filePoint + getFileName()));
                writer.write(buffer.toString());
                writer.close();
                filePoint++;
                buffer = new StringBuffer();
            }
            line = reader.readLine();
        }
        writer = new BufferedWriter(new FileWriter(getOutPutPath() + "//" + filePoint + getFileName()));
        writer.write(buffer.toString());
        writer.close();
        JOptionPane.showMessageDialog(this, "文件分割完毕", "确认对话框", JOptionPane.WARNING_MESSAGE);
    } catch (IOException ex) {
        Logger.getLogger(SpiderEditorUI.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(SpiderEditorUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}                                        

private void BtnChooseActionPerformed(java.awt.event.ActionEvent evt) {                                          
    JFileChooser chooser = new JFileChooser();
    chooser.setSize(new Dimension(200, 200));
    chooser.setVisible(true);
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    if(!file.getPath().endsWith("txt")){
         JOptionPane.showMessageDialog(this, "只支持TXT文件,请您选择正确格式的文件", "确认对话框",JOptionPane.WARNING_MESSAGE);
         return;
    }
    setFilePath(file.getPath());
    setFileName(file.getName());
    TxtInputAddress.setText(file.getPath());
}                                         

    /**
     *
     * @param evt
     */
private void BtnDestinationActionPerformed(java.awt.event.ActionEvent evt) {                                               
     JFileChooser chooser = new JFileChooser();
    chooser.setSize(new Dimension(200, 200));
    chooser.setVisible(true);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    setOutPutPath(file.getPath());
    TxtOutPutAddress.setText(file.getPath());
}                                              

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SpiderEditorUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton BtnChoose;
    private javax.swing.JButton BtnDestination;
    private javax.swing.JButton BtnSplit;
    private javax.swing.JTextField TxtFileSize;
    private javax.swing.JTextField TxtInputAddress;
    private javax.swing.JTextField TxtOutPutAddress;
    private javax.swing.JLabel labelTxt;
    // End of variables declaration                   
}
原创粉丝点击