java带进度条的大文件读取程序

363 阅读2分钟

先祭出代码 项目一共包括两个类,以及一个swing界面文件

主文件类 FileSplit.java

package homework.BigFile;


import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.Objects;

class ThreadDemo extends Thread {
    private Thread t;
    private String threadName;
    boolean suspended;


    ThreadDemo(String name) {
        threadName = name;
        System.out.println("Creating " + threadName);
    }

    public void run() {
        System.out.println("Running " + threadName);
        Global.Reader = new FileSplit();
        try {

            Global.Reader.ReadFile("src/homework/BigFile/userid_profile.txt");
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }

        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

    /**
     * 暂停
     */
    public void Suspend() throws InterruptedException {
        System.out.println("暂停");
        t.suspend();
    }

    /**
     * 继续
     */
    public synchronized void Resume() {
        System.out.println("继续");
        t.resume();
    }
}

class ThreadDemo2 extends Thread {
    private Thread t;
    private String threadName;
    boolean suspended;
    JProgressBar progressBar;


    ThreadDemo2(String name, JProgressBar j) {
        threadName = name;
        System.out.println("Creating " + threadName);
        this.progressBar = j;

    }

    public void run() {
        System.out.println("开始执行进度条");
//        this.progressBar.setMaximum((int) Global.lines);
        while (true) {
            this.progressBar.setValue(Global.JDT);
            System.out.println(Global.JDT);
            if (Global.JDT >= 3350000) {
                break;
            }
        }

    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

    /**
     * 暂停
     */
    public void Suspend() throws InterruptedException {
        System.out.println("暂停");
        t.suspend();
    }

    /**
     * 继续
     */
    public synchronized void Resume() {
        System.out.println("继续");
        t.resume();
    }
}

public class FileSplit {
    private JPanel root;
    private JProgressBar progressBar1;
    private JTextField textField1;
    private JButton 开始读取Button;
    private JButton 暂停继续Button;
    private JButton 结束Button;
    File file;
    FileInputStream fileInputStream;
    BufferedInputStream bufferedInputStream;
    InputStreamReader inputStreamReader;
    BufferedReader input;


    public FileSplit() {
        ThreadDemo2 t2 = new ThreadDemo2("进度条", progressBar1);
        t2.start();
        开始读取Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Global.t1 = new ThreadDemo("读取线程");
                Global.t1.start();

            }
        });
        暂停继续Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (Objects.equals(Global.status, "1")) {
                    try {
                        Global.t1.Suspend();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    Global.status = "0";
                    暂停继续Button.setText("继续");


                } else if (Objects.equals(Global.status, "0")) {
                    Global.t1.Resume();
                    Global.status = "1";
                     暂停继续Button.setText("暂停");
                }


            }
        });

    }

    void ReadFile(String src) throws IOException, InterruptedException {
        long timer = System.currentTimeMillis();
        int bufferSize = 20 * 1024 * 1024;//设读取文件的缓存为20MB

        //建立缓冲文本输入流
        file = new File(src);
//        long lines = Files.lines(Paths.get(new File(src).getPath())).count();
//        Global.lines = lines;
        fileInputStream = new FileInputStream(file);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        inputStreamReader = new InputStreamReader(bufferedInputStream);
        input = new BufferedReader(inputStreamReader, bufferSize);

        int splitNum = 2 - 1;//要分割的块数减一
        int fileLines = 3355000;//输入文件的行数
        long perSplitLines = fileLines / splitNum;//每个块的行数
        for (int i = 1; i <= splitNum; ++i) {
            //分割
            //每个块建立一个输出
            FileWriter output = new FileWriter("src/homework/BigFile/part/part" + i + ".txt");
            String line = null;
            System.out.println("1");
            //逐行读取,逐行输出
            for (long lineCounter = 0; lineCounter < perSplitLines && (line = input.readLine()) != null; ++lineCounter) {
                output.append(line).append("\r");
                Global.JDT = (int) lineCounter;
            }
            output.flush();
            output.close();
            output = null;
        }
        input.close();
        timer = System.currentTimeMillis() - timer;
        System.out.println("处理时间:" + timer);


    }

    public static void main(String[] args) throws IOException, InterruptedException {
        try {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;

            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        JFrame frame = new JFrame("大文件读取程序张建宏");
        frame.setContentPane(new FileSplit().root);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setLocation(200, 200);


    }
}

剩下的是一个存放全局变量的类 Global.java

package homework.BigFile;

public class Global {
    public static String status = "1";
    public static FileSplit Reader;
    public static int Zanting = 0;
    public static ThreadDemo t1;
    public static int JDT = 0;
    public static long lines = 1000000;
}

最后上运行界面

image.png

image.png

image.png

image.png

image.png