使用for循环保存多个.txt文件 [英] Saving multiple .txt Files with a for Loop

查看:71
本文介绍了使用for循环保存多个.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存.txt文件的循环时遇到问题.它总是将最后一个文件保存两次(如果我将 2放入for循环)

I have a Problem with my saving Loop of my .txt-Files. It always saves the last File twice (if I put in 2 for the for loop)

当前,我尝试使用 int WritingLength = fc.getSelectedFiles().length; ,然后导致总共没有保存选项.欣赏任何可以帮助我解决问题的想法,因为我对Java还是很陌生.在这里,整个代码被摘录以更好地概述我的代码:

Currently I tried using int writingLength = fc.getSelectedFiles().length;, which then results in no saving Option in total. Appreciate any thought that could help me to solve my problem, since I'm fairly new to Java. Here the whole code snipped to give a better overview of my code:

    private void prepareTDDExcelDoc() {
        fc.setMultiSelectionEnabled(true);
        fc.resetChoosableFileFilters();
        fc.setAcceptAllFileFilterUsed(false);
        final FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel document (*.csv)", "csv");
        fc.setFileFilter(filter);
        int writingLength = fc.getSelectedFiles().length;
        for(int i=0;i<writingLength;i++) {
            final String suggestedFilename = reportName.replaceAll("\\W+", "_") + ".xlsx";
            fc.setSelectedFile(new File(suggestedFilename));
            final int returnVal = fc.showSaveDialog(frmCognosTddBuilder);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                    enableUserInteraction(false);
                    outputFilename = fc.getSelectedFile().getCanonicalPath();
                    final ExportedOutputInterface exportedExcelOutput = new ExcelOutput(this, outputFilename);
                    
                    progressMonitor = new ProgressMonitor(getFrame(), "Builing TDD", "", 0, 100);
                    
                    task = new ReportDataExtracter(this, exportedExcelOutput);
                    task.addPropertyChangeListener(new PropertyChangeListener() {

                        @Override
                        public void propertyChange(final PropertyChangeEvent evt) {
                            if ("progress" == evt.getPropertyName()) {
                                final int progress = (Integer) evt.getNewValue();
                                progressMonitor.setProgress(progress);
                                final String message = String.format("Completed %d%%.\n", progress);
                                progressMonitor.setNote(message);
                                if (progressMonitor.isCanceled() || task.isDone()) {
                                    if (progressMonitor.isCanceled()) {
                                        task.cancel(true);
                                    } else {
                                        progressMonitor.close();
                                    }
                                }
                            }
                        }
                    });
                    task.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

推荐答案

我看到此"reportName"变量,该变量似乎未在此函数中声明,因此我想它是一个实例变量.无论如何,您都需要对该值进行小的转换,并将其设置为列表中所有条目的输出文件名.我想象输出文件只会在列表中有最后一个文件.

I see this "reportName" variable, which doesn't appear to be declared in this function, so I suppose it's an instance variable. In any case, you're doing a small transformation on that value and setting it as the output file name for ALL the entries of the list. I imagine the output file is only ever going to have the last file in the list.

在调试器中执行这样的代码真的很有帮助,因此您可以逐步执行它并观察变量的值.在代码创建UI组件时,您可能需要使用两个显示.自从我不得不调试UI代码已经过去了几年.

It's really helpful to execute code like this in the debugger, so you can step through it and watch the values of variables. As your code is creating UI components, you'll probably need to use two displays. It's been a few years since I had to debug UI code.

这篇关于使用for循环保存多个.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆