gpt4 book ai didi

java - 加载大文件时如何提高JTextPane的性能

转载 作者:行者123 更新时间:2023-11-30 11:15:15 25 4
gpt4 key购买 nike

我想使用 JTextPane 加载一个大文件(1MB 的纯文本)内容。加载一个大文件用了将近两分钟。我想在几秒钟内将大文件加载到 JTextPane 中。如果有可能提高 JTextPane 的性能。我的打开操作代码在 openActionPerformed() 方法中可用。请检查并给我一些建议。谢谢。

构造函数代码:

public class OpenDemo extends javax.swing.JFrame {
JTextPane textPane;
JScrollPane scrollPane;
int i=0;
public OpenDemo() {
initComponents();
textPane=new JTextPane();
}

OpenActionPerformed() 方法:

    private void openActionPerformed(java.awt.event.ActionEvent evt) {                                      
int offset = 0;
FileDialog fd = new FileDialog(OpenDemo.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);
String title;
String path;
Path filePath = null;
File file;
if (fd.getFile() != null) {
path = fd.getDirectory() + fd.getFile();
file=new File(path);
filePath=file.toPath();
title=fd.getFile();
JInternalFrame internalFrame = new JInternalFrame("",true,true);
i++;
internalFrame.setName("Doc "+i);
internalFrame.setTitle(title);
scrollPane=new JScrollPane(textPane);
internalFrame.add(scrollPane);
tp.add(internalFrame);

myOffsetTextField=new JTextField();
List<String> allLines = null;
try {
allLines = Files.readAllLines(filePath, Charsets.UTF_8);
}
catch (MalformedURLException ex) {
Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
}

try{
offset = Integer.parseInt(myOffsetTextField.getText());
}
catch(NumberFormatException ne){
}
int numberOfLinesToShow = 10000;
int start = Math.min(allLines.size(), offset);
int end = Math.min(allLines.size(), start + numberOfLinesToShow);
List<String> sublist = allLines.subList(start, end);
textPane.setText(Joiner.on('\n').join(sublist));
textPane.setCaretPosition(0);
}

主要方法:

public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new OpenDemo().setVisible(true);
}
});
}
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tp;
}

最佳答案

对于 1 MB 的文本文件,除非从软盘或类似设备中读取,否则不可能花两分钟加载。

将其全部放入用户界面是胡说八道,没有人可以用它做任何事情。使用滚动条滚动也完全无法使用。允许用户输入起始偏移量(以行为单位),使用 Files.readLines 读取文件进入 List<String> , 只显示几行。

代码思路

所有非 JDK 类都来自 Guava。

List<String> allLines = Files.readLines(file, Chatsets.UTF8);
int offset = Integer.parseInt(myOffsetTextField.getText());
int numberOfLinesToShow = 10000;
int start = Math.min(allLines.size(), offset);
int end = Math.min(allLines.size(), start + numberOfLinesToShow);

// a sane-sized list of at most `numberOfLinesToShow` lines
List<String> sublist = allLines.sublist(start, end);

textPane.setText(Joiner.on('\n').join(sublist));

关于java - 加载大文件时如何提高JTextPane的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25541571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com