【问题标题】:CSV file reader taking too long in window builderCSV 文件阅读器在窗口构建器中花费的时间太长
【发布时间】:2013-12-22 20:49:39
【问题描述】:

所以我有一个能够读取 csv 文件的工作代码,但由于文件非常大,因此在所有数据立即显示在 textarea 中之前,大约需要两分钟才能读取。我在 Eclipse 中使用带有 windowsbuilder 的 GUI 界面。下面是代码;

JButton btnopen = new JButton("Open");
    btnopen.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{

                final JFileChooser fc = new JFileChooser(); //launching the file chooser
                fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt")); //this will allow text files to be read
                fc.setFileFilter(new FileNameExtensionFilter("CSV", "csv")); //this will allow csv files to be read
                fc.setFileFilter(new FileNameExtensionFilter("JSON", "json")); //this will allow json files to be read
                fc.setFileFilter(new FileNameExtensionFilter("XML", "xml")); //this will allow xml files to be read

                int returnVal = fc.showOpenDialog(contentPane);
                File f; //file that holds the data from the text file
                fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    f = fc.getSelectedFile(); //tells file chooser to get the file selected and store into file variable
                    String output="";
                    //use buffered reader and file reader to read selected file
                    BufferedReader in = new BufferedReader(new FileReader(f));
                    //after reading data, store in to string
                    String line = in.readLine(); //every time a line is read, data is put into text area
                    int i=0;
                    while(line!=null){ //while still reading...
                        //
                        line = in.readLine(); //continue reading next line of file
                        output +=line+"\n";
                        //textArea.append(line +"\n"); //add text from file into text area
                        //++i;
                    }

                    textArea.append(output);
                }
            }
        catch(Exception e){

        }

        }           
    });

【问题讨论】:

  • 那么你的问题是什么?
  • 使用 StringBuilder 构建字符串而不是 output+=line;
  • @HectorLector 你能否告诉我如何在我的代码中实现它。我是新手,所以我对 java 并不完全有信心。
  • @SpacePope 我的问题是,我想知道我目前的代码是否有任何改进,可以更快地读取 csv 文件。
  • 要将整个文件内容读入字符串,您可以使用: String fullFile = new String(Files.readAllBytes(f.toPath()));您正在阅读的文件的大小是多少?

标签: java eclipse csv windowbuilder


【解决方案1】:

@HectorLector 的回应会稍微优化阅读,但他们是对的;最终,读取文件将花费尽可能长的时间。我怀疑您的潜在问题可能是“在阅读此文件时如何使我的 UI 响应?” - 现在,由于您在 ActionListener 中进行读取,因此在读取文件时您的 UI 将完全被阻止,这会导致糟糕的用户体验。

这个问题的答案是,像文件系统访问这样的长时间运行的操作应该在后台线程上完成。考虑使用SwingWorker (see the tutorial) 来完成这样的任务;工作线程可以构建字符串并使用done() 方法更新文本区域(或者,如果您想在读取文件时显示文件,请使用publish()process() 方法的组合)。

另外,作为旁注,请确保您使用的是 close()ing 来读取文件的 BufferedReader。将阅读本身包裹在try 块中,并关闭在finally 内,以防阅读过程中出现任何问题。像这样的:

BufferedReader in = new BufferedReader(new FileReader(f));
StringBuilder output = new StringBuilder();
try {
    while (in.hasNext()) {
        output.append(in.readLine());
        output.append("\n");
    }
finally {
    in.close();
}

【讨论】:

    【解决方案2】:

    使用 StringBuilder 创建您的字符串。因为字符串是不可变的,所以每次使用“+”附加内容时都会创建一个新对象。
    这将提高性能,但可能不是主要问题。 文件系统访问很慢。也许尝试一次读取您的文件并将其保存在内存中(在列表或其他内容中)。或者尝试一次只显示文件的一部分(分页)。

                    BufferedReader in = new BufferedReader(new FileReader(f));
                    StringBuilder builder = new StringBuilder();
                    String line = "";
    
                    while((line = in.readLine()) != null){ 
                         builder.append(line);
                         builder.append("\n");
                    } 
    
                    textArea.append(builder.toString());
    

    【讨论】:

    • 我现在就试试这个。我应该把它放在哪里 output+=line;是并用您的代码替换它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    相关资源
    最近更新 更多