Java实现大文件的分割与合并的方法详解

2022-11-13 14:11:45 合并 分割 详解

一、题目描述-合并多个文本文件

1、题目

题目:做一个合并多个文本文件的工具

2、解题思路

创建一个类:TextFileConcatenation

使用TextFileConcatenation继承JFrame构建窗体

读取文本文件时,用的是BufferedReader类的readLine()方法读入一行数据。

将选择的多个文本文件合并到d://concatenation.txt

3、代码详解

package com.xiaoxuzhu;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

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.jscrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class TextFileConcatenation extends JFrame {

    
    private JPanel contentPane;
    private JTextField textField;
    private File[] textFiles;
    private JTextArea textArea;

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TextFileConcatenation frame = new TextFileConcatenation();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public TextFileConcatenation() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel fileLabel = new JLabel("文件夹所在位置");
        panel.add(fileLabel);

        textField = new JTextField();
        panel.add(textField);
        textField.setColumns(10);

        JButton button = new JButton("选择文件夹");
        button.addActionListener(new ActionListener() {
            public void actionPerfORMed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        panel.add(button);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        textArea = new JTextArea();
        scrollPane.setViewportView(textArea);

        JPanel buttonPanel = new JPanel();
        contentPane.add(buttonPanel, BorderLayout.SOUTH);

        JButton concatButton = new JButton("合并文件");
        concatButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_concatButton_actionPerformed(e);
            }
        });
        buttonPanel.add(concatButton);
    }

    protected void do_button_actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setMultiSelectionEnabled(false);
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectFile = chooser.getSelectedFile();
            textField.setText(selectFile.getAbsolutePath());
            textFiles = selectFile.listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {
                    if (pathname.getAbsolutePath().endsWith(".txt")) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });
            for (File textFile : textFiles) {
                textArea.append(textFile.getAbsolutePath() + "\n\r");
            }
        }
    }

    protected void do_concatButton_actionPerformed(ActionEvent e) {
        if (textFiles == null) {
            JOptionPane.showMessageDialog(this, "请选择文件文件位置!", "警告信息", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (textFiles.length == 0) {
            JOptionPane.showMessageDialog(this, "文件夹中不包括文本文件!", "警告信息", JOptionPane.WARNING_MESSAGE);
            return;
        }
        BufferedReader reader = null;
        FileWriter writer = null;
        try {
            writer = new FileWriter("d://concatenation.txt");// 创建文件输出流
            for (File textFile : textFiles) {// 遍历用户选择的文本文件
                reader = new BufferedReader(new FileReader(textFile));// 创建缓冲输入流
                String line;
                while ((line = reader.readLine()) != null) {
                    writer.write(line);// 将读入的数据写入到文件中
                }
            }
            JOptionPane.showMessageDialog(this, "文件合并成功!", "提示信息", JOptionPane.INFORMATION_MESSAGE);
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

启动:

合并前:选择文件夹

合并后:

二、题目描述-对大文件进行分割处理

1、题目

题目:大数据文件为了方便传输,通常会进行切割处理。

实现一个将大文件分割成多个文件的工具

2、解题思路

创建一个类:ComminuteFrame

使用ComminuteFrame继承JFrame构建窗体

ComminuteFrame 增加标签,文本框和分割按钮

创建一个类:ComminuteUtil

ComminuteUtil主要处理分割的逻辑处理

通过输入流读取要分割的文件,分别从流中读取相应的字节数;

将其写到.tem的后缀文件中,分割的文件存放在源文件的对应目录下。

3、代码详解

ComminuteFrame类

package com.xiaoxuzhu;

import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class ComminuteFrame extends JFrame {

    
    private static final long serialVersionUID = 2547743180459668116L;
    private JPanel contentPane;
    private JTextField sourceTextField;
    private JTextField sizeTextField;

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    ComminuteFrame frame = new ComminuteFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public ComminuteFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 430, 211);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setTitle("文件分割");
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 414, 181);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel messagelabel = new JLabel("源文件:");
        messagelabel.setBounds(47, 41, 54, 20);
        panel.add(messagelabel);

        sourceTextField = new JTextField();
        sourceTextField.setBounds(105, 41, 178, 21);
        panel.add(sourceTextField);
        sourceTextField.setColumns(10);

        JButton sourceButton = new JButton("选择");
        sourceButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                do_sourceButton_actionPerformed(arg0);
            }
        });
        sourceButton.setBounds(303, 40, 67, 23);
        panel.add(sourceButton);

        JLabel sizelabel = new JLabel("分割大小:");
        sizelabel.setBounds(34, 86, 67, 15);
        panel.add(sizelabel);

        sizeTextField = new JTextField();
        sizeTextField.setBounds(105, 83, 178, 21);
        panel.add(sizeTextField);
        sizeTextField.setColumns(10);
        sizeTextField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent event) { // 某键按下时调用的方法
                char ch = event.geTKEyChar(); // 获取用户键入的字符
                if ((ch < '0' || ch > '9')) { // 如果用户输入的信息不为数字或小数
                    event.consume(); // 不允许用户键入
                }

            }
        });

        JLabel lblM = new JLabel("M");
        lblM.setBounds(313, 86, 44, 15);
        panel.add(lblM);

        JButton cominButton = new JButton("分割");
        cominButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                do_cominButton_actionPerformed(arg0);
            }
        });
        cominButton.setBounds(101, 138, 93, 23);
        panel.add(cominButton);

        JButton close = new JButton("退出");
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                do_close_actionPerformed(arg0);
            }
        });
        close.setBounds(229, 138, 93, 23);
        panel.add(close);
    }

    protected void do_sourceButton_actionPerformed(ActionEvent arg0) {
        java.awt.FileDialog fd = new FileDialog(this);
        fd.setVisible(true);
        String path = fd.getDirectory() + fd.getFile();
        if (!path.equals("") && !(path == null)) {
            sourceTextField.setText(path);
        }
    }

    protected void do_cominButton_actionPerformed(ActionEvent arg0) {
        ComminuteUtil util = new ComminuteUtil();
        String path = sourceTextField.getText();
        int size = Integer.parseInt(sizeTextField.getText());
        String subPath = path.substring(0, path.lastIndexOf("\\"));
        util.fenGe(new File(path), new File(subPath), size);
        JOptionPane.showMessageDialog(getContentPane(), "文件分割成功!", "信息提示框", JOptionPane.PLAIN_MESSAGE);
    }

    protected void do_close_actionPerformed(ActionEvent arg0) {
        System.exit(0);
    }
}

ComminuteUtil类

package com.xiaoxuzhu;
import java.io.*;



public class ComminuteUtil {
    // 实现文件分割方法
    public void fenGe(File commFile, File untieFile, int filesize) {
        FileInputStream fis = null;
        int size = 1024 * 1024; // 用来指定分割文件要以MB为单位
        try {
            if (!untieFile.isDirectory()) { // 如果要保存分割文件地址不是路径
                untieFile.mkdirs(); // 创建该路径
            }
            size = size * filesize;
            int length = (int) commFile.length(); // 获取文件大小
            int num = length / size; // 获取文件大小除以MB的得数
            int yu = length % size; // 获取文件大小与MB相除的余数
            String newfengeFile = commFile.getAbsolutePath(); // 获取保存文件的完成路径信息
            int fileNew = newfengeFile.lastIndexOf(".");
            String strNew = newfengeFile.substring(fileNew, newfengeFile.length()); // 截取字符串
            fis = new FileInputStream(commFile); // 创建FileInputStream类对象
            File[] fl = new File[num + 1]; // 创建文件数组
            int begin = 0;
            for (int i = 0; i < num; i++) { // 循环遍历数组
                fl[i] = new File(untieFile.getAbsolutePath() + "\\" + (i + 1) + strNew + ".tem"); // 指定分割后小文件的文件名
                if (!fl[i].isFile()) {
                    fl[i].createNewFile(); // 创建该文件
                }
                FileOutputStream fos = new FileOutputStream(fl[i]);
                byte[] bl = new byte[size];
                fis.read(bl); // 读取分割后的小文件
                fos.write(bl); // 写文件
                begin = begin + size * 1024 * 1024;
                fos.close(); // 关闭流
            }
            if (yu != 0) { // 文件大小与指定文件分割大小相除的余数不为0
                fl[num] = new File(untieFile.getAbsolutePath() + "\\" + (num + 1) + strNew + ".tem"); // 指定文件分割后数组中最后一个文件名
                if (!fl[num].isFile()) {
                    fl[num].createNewFile(); // 新建文件
                }
                FileOutputStream fyu = new FileOutputStream(fl[num]);
                byte[] byt = new byte[yu];
                fis.read(byt);
                fyu.write(byt);
                fyu.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

分割前:

分割后:

三、题目描述-分割后又再次合并

1、题目

题目:分割后的文件要能再次正常使用,需要合并。

实现:做一个把分割后的文件又再次合并的工具

2、解题思路

创建一个类:UniteFrame

使用UniteFrame继承JFrame构建窗体

UniteFrame增加标签,文本框和分割按钮

创建一个类:UniteUtil

UniteUtil主要处理合并的逻辑处理

通过文件字节输入/输出流,把要合并的所有文件读取后,统一写入新文件中。

3、代码详解

UniteFrame类:

package com.xiaoxuzhu;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;


public class UniteFrame extends JFrame {

    
    private JPanel contentPane;
    JList fileList = new JList();
    DefaultListModel listModel = new DefaultListModel();

    String folder = "";
    String fileName = "";
    List<String> listPath = new ArrayList<String>();

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UniteFrame frame = new UniteFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public UniteFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 425, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setTitle("文件合并");
        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 409, 262);
        contentPane.add(panel);
        panel.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(33, 51, 311, 151);
        panel.add(scrollPane);

        fileList.setModel(listModel);
        scrollPane.setViewportView(fileList);

        JLabel messagelabel = new JLabel("要进行合并的文件列表:");
        messagelabel.setBounds(33, 20, 156, 21);
        panel.add(messagelabel);

        JButton openButton = new JButton("打开");
        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_openButton_actionPerformed(arg0);
            }
        });
        openButton.setBounds(43, 218, 68, 23);
        panel.add(openButton);

        JButton uniteButton = new JButton("合并");
        uniteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_uniteButton_actionPerformed(arg0);
            }
        });
        uniteButton.setBounds(152, 218, 68, 23);
        panel.add(uniteButton);

        JButton closeButton = new JButton("退出");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                do_closeButton_actionPerformed(arg0);
            }
        });
        closeButton.setBounds(258, 218, 68, 23);
        panel.add(closeButton);

    }

    // 打开按钮的单击事件
    @SuppressWarnings("rawtypes")
    protected void do_openButton_actionPerformed(ActionEvent arg0) {
        java.awt.FileDialog fd = new FileDialog(this);
        fd.setVisible(true);
        folder = fd.getDirectory();
        String path = fd.getDirectory() + fd.getFile();
        fileName = fd.getFile();
        UniteUtil util = new UniteUtil();
        if (path.endsWith(".tem")) {
            List list = util.getList(folder);
            for (int i = 0; i < list.size(); i++) {
                String abpath = list.get(i).toString();
                if (abPath.endsWith(".tem")) {
                    System.out.println("ABPAth " + abPath);
                    listModel.addElement(abPath);
                    listPath.add(abPath);
                }
            }
        }
        validate();
    }

    // 合并按钮的单击事件
    protected void do_uniteButton_actionPerformed(ActionEvent arg0) {
        UniteUtil util = new UniteUtil();
        String newName = fileName.substring(fileName.indexOf("."), fileName.lastIndexOf("."));
        File[] files = new File[listPath.size()];
        for (int i = 0; i < listPath.size(); i++) {
            files[i] = new File(listPath.get(i).toString());
        }
        util.unite(files, new File(folder), newName);
        JOptionPane.showMessageDialog(getContentPane(), "文件合并成功!", "信息提示框", JOptionPane.PLAIN_MESSAGE);
    }

    // 退出按钮的单击事件
    protected void do_closeButton_actionPerformed(ActionEvent arg0) {
        System.exit(0);
    }
}

UniteUtil类

package com.xiaoxuzhu;
import java.io.*;
import java.util.*;



public class UniteUtil {
    
    public void unite(File[] file, File cunDir, String hz) {
        try {
            File heBingFile = new File(cunDir.getAbsoluteFile() + "\\UNTIE" + hz); // 指定分割后文件的文件名
            if (!heBingFile.isFile()) {
                heBingFile.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(heBingFile); // 创建FileOutputStream对象
            for (int i = 0; i < file.length; i++) { // 循环遍历要进行合并的文件数组对象
                FileInputStream fis = new FileInputStream(file[i]);
                int len = (int) file[i].length(); // 获取文件长度
                byte[] bRead = new byte[len];
                fis.read(bRead); // 读取文件
                fos.write(bRead); // 写入文件
                fis.close(); // 将流关闭
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 获取磁盘所有文件方法
    public List<String> getList(String path) {
        LinkedList<File> list = new LinkedList<File>();
        ArrayList<String> listPath = new ArrayList<String>();
        File dir = new File(path);
        File file[] = dir.listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory())
                list.add(file[i]);
            else {
                listPath.add(file[i].getAbsolutePath());
            }
        }
        File tmp;
        while (!list.isEmpty()) {
            tmp = list.removeFirst(); // 移除并返回集合中第一项
            if (tmp.isDirectory()) {
                file = tmp.listFiles();
                if (file == null)
                    continue;
                for (int i = 0; i < file.length; i++) {
                    if (file[i].isDirectory())
                        list.add(file[i]);
                    else {
                        listPath.add(file[i].getAbsolutePath());
                    }

                }
            } else {

            }
        }
        return listPath;
    }

}

合并成功:

4、多学一个知识点

新合并的文件,是怎么知道后缀文件名是哪个

这个在进行分割处理时就有考虑设计了,切割的文件名是以1.zip.tem,前面的是切割排序号,中间的是源文件的文件类型后缀,最后是切割文件后缀。

String newName = fileName.substring(fileName.indexOf("."), fileName.lastIndexOf("."));

到此这篇关于Java实现大文件的分割与合并的方法详解的文章就介绍到这了,更多相关Java文件分割 合并内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章