天天看點

Java 實時讀取檔案

package nc.fei.util;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
 /**
  * 
  * @author xiangfeifei
  *
  * 2020年6月21日
  */
public class ReadLog extends JPanel implements ActionListener {
	private static final long serialVersionUID = 8133545024833322343L;
	private LineNumberReader readLine = null;
    private JFileChooser fc;
    private JButton startButton;
    private JTextArea taskOutput;
    private Task task;
    private JLabel path;
    class Task extends SwingWorker<Void, Void> {
        private File file;
    	public Task(File file) {
			this.file = file;
		}

        @Override
        public Void doInBackground() {
        	try {
    			readLine = new LineNumberReader(new FileReader(file));						
    			while(readLine.readLine() != null){}
    			while(true){
    				String line = readLine.readLine();
    				if(line != null){
    					taskOutput.append(line+"\n");
    					taskOutput.setCaretPosition(taskOutput.getDocument().getLength());
    				}
    			}
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    			try {
    				readLine.close();
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    		}
            return null;
        }
 
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            startButton.setEnabled(true);
            setCursor(null); 
            taskOutput.append("Done!\n");
        }
    }
 
    public ReadLog() {
        super(new BorderLayout());
       
        startButton = new JButton("加載");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
        
        path = new JLabel();
        
        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5,5,5,5));
        taskOutput.setEditable(false);
        taskOutput.setLineWrap(true);
        
        JPanel panel = new JPanel();
        panel.add(startButton);
        panel.add(path);
 
        add(panel, BorderLayout.PAGE_START);
        add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
 
    }
 
    public void actionPerformed(ActionEvent evt) {
        startButton.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        fc = new JFileChooser();
        int value = fc.showOpenDialog(this);
        if(value == JFileChooser.APPROVE_OPTION){  
        	File file = fc.getSelectedFile();
        	String fpath = file.getPath();
        	path.setText(fpath);
        	task = new Task(file);
        	task.execute();
        }
    }
 
    private static void createAndShowGUI() {
        
        JFrame frame = new JFrame("實時日志");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(800, 600));
        frame.setLocationRelativeTo(frame.getParent());
        
        JComponent newContentPane = new ReadLog();
        newContentPane.setOpaque(true); 
        frame.setContentPane(newContentPane);
 
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

           

因為要在window環境,實時的檢視日志,是以寫了這麼個小程式