问题描述
我试图读取一个动态更改的文件,并在jTextArea中显示其内容。
我正在使用缓冲读取器进行读取。
我试图在控制台上显示内容,它工作正常,但不能用于jTextArea。 / p>
我正在做类似于
while(true)
{
line = br.readLine();
if(line == null)
Thread.sleep();
else
System.out.println(line);
任何帮助将不胜感激。谢谢
不要使用 Thread.sleep()
将阻止美国东部时间。如果您想动画结果,请使用 javax.swing.Timer
。只读每一个计时器的迭代,直到它到达文件的结尾,然后停止计时器
timer = new (100,new ActionListener(){
public void actionPerformed(ActionEvent e){
String line;
if((line = reader.readLine())! = null){
textArea.append(line +\\\
);
} else {
((Timer)e.getSource())。stop(); $ b $ (b)
} catch(IOException ex){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
测试这个程序。我认为它的工作方式是你想要的。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
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.JTextField;
导入javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ReadFile {
File file = null;
BufferedReader reader = null;
定时器定时器=空;
private JTextArea textArea;
private JTextField jtfFile;
private String fileName;
私人JButton浏览;
私有JFrame框架;
public ReadFile(){
textArea = new JTextArea(25,60);
frame = new JFrame(Show Log);
browse = new JButton(Browse);
browse.addActionListener(new ShowLogListener());
jtfFile = new JTextField(25);
jtfFile.addActionListener(new ShowLogListener());
$ b $ timer = new Timer(100,new ActionListener(){
public void actionPerformed(ActionEvent e){
String line;
try {
if ((line = reader.readLine())!= null){
textArea.append(line +\\\
);
} else {
((Timer)e.getSource ())。stop();
}
} catch(IOException ex){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex );
}
}
});
JPanel panel = new JPanel();
panel.add(new JLabel(File:));
panel.add(jtfFile);
panel.add(浏览);
frame.add(panel,BorderLayout.NORTH);
frame.add(new JScrollPane(textArea),BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
private class ShowLogListener实现ActionListener {
$ b $ @Override
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser ();
int result = chooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
file = chooser.getSelectedFile();
fileName = file.getName();
jtfFile.setText(fileName);
尝试{
reader = new BufferedReader(new FileReader(file));
} catch(FileNotFoundException ex){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}
timer.start();
$ b public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new ReadFile();
}
});
I am trying to read a file that changes dynamically and display its contents in jTextArea.
I am using buffered reader for reading.
I have tried to display the contents on console and it works fine, but doesn't work with jTextArea.
I am doing something like
while(true)
{
line = br.readLine();
if(line == null)
Thread.sleep();
else
System.out.println(line);
}
Any help would be appreciated. Thanks
解决方案 Don't use Thread.sleep()
, you will block the EDT. Instead use a javax.swing.Timer
if you want to "animate" the results. Just read each line each iteration of the timer, until it reaches the end of the file, then stop the timer
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
String line;
try {
if ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
} else {
((Timer) e.getSource()).stop();
}
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Test this program out. I think it works the way you want it to.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
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.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ReadFile {
File file = null;
BufferedReader reader = null;
private Timer timer = null;
private JTextArea textArea;
private JTextField jtfFile;
private String fileName;
private JButton browse;
private JFrame frame;
public ReadFile() {
textArea = new JTextArea(25, 60);
frame = new JFrame("Show Log");
browse = new JButton("Browse");
browse.addActionListener(new ShowLogListener());
jtfFile = new JTextField(25);
jtfFile.addActionListener(new ShowLogListener());
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
String line;
try {
if ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
} else {
((Timer) e.getSource()).stop();
}
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
JPanel panel = new JPanel();
panel.add(new JLabel("File: "));
panel.add(jtfFile);
panel.add(browse);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private class ShowLogListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
fileName = file.getName();
jtfFile.setText(fileName);
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
}
timer.start();
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ReadFile();
}
});
}
}
这篇关于读取一个日志文件并将其显示在jTextArea中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!