1

I've got a problem with OpenCSV lib; precisely, with writing Strings into the file itself.

public class Calc {
    public static Summary summary;
    public static CSVWriter writer;
    (...)
        someButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                (...)
                summary = new Summary();
                writer = summary.createCSV();
                someThreadedMethod();
                summary.closeWriter(writer);
                } catch (...) {}
            }
        }
    }
}

Summary class:

public class Summary {
    private static int version = 0;
    private static String name;

    public Summary() throws IOException {
        this.version += 1;
        this.name = "Summary" + Integer.toString(version) + ".csv";
    }

    public CSVWriter createCSV() throws IOException {
        CSVWriter writer = new CSVWriter(new FileWriter(name));
        return writer;
    }

    public void addInfo(CSVWriter writer, String info) {
        String[] record = info.split("#");
        writer.writeNext(record);
    }

    public void closeWriter(CSVWriter writer) throws IOException {
        writer.close();
    }
}

And someThreadedMethod() creates another classes objects which log into given website and parse given Strings form tables:

(...)
ArrayList<String> contentStrToPrint = new ArrayList<>(Arrays.asList(someStringArray));

for (int i = 7; i < contentStrToPrint.size(); i += 7) {
                for (String aSomething : something) {
                    if (contentStrToPrint.get(i).contains(aSomething)) {
                        StringBuilder summary = new StringBuilder();
                        for (int j = i; j < i + 7; j++) {
                            row.add(contentStrToPrint.get(j));
                            if (j != i + 6)
                                summary.append(contentStrToPrint.get(j)).append("#");
                            else
                                summary.append(contentStrToPrint.get(j));
                        }
                        data.add(row);
                        Calc.summary.addInfo(Calc.writer, summary.toString());
                        row = new Vector<>();
                    }
                }
            }

I know it's not the neatest code. As a result I get empty "SummaryN.csv" file. I can't see a mistake here; moreover, I recreated this in Test project - much simpler, only writing Strings from given ArrayList (also being made from given String[]) on buttonClick - and it works. Also, printing out values at any possible stage returns proper Strings. Is it possible to be somehow (I have no idea how) be connected with the threaded method?

Any help would be much appreciated!

PS The fun with Vectors is unimportant and needed in order to show some values.

Canert
  • 23
  • 6
  • What is `data`? Even though the row is a synchronized `Vector` class, `data` needs to be a synchronized as well otherwise the one thread that creates the row won't publish it appropriately so other threads will see it. Lastly, `Vector` is an old class and should not be used. See: https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Gray Sep 05 '17 at 20:30
  • Vector isn't really my problem (which is already solved), but if you need to know it's a case of convenience - it's easy to make a table out of Vectors. These aren't very consuming in my case. – Canert Sep 15 '17 at 21:42

1 Answers1

0

Assuming that anytime you go into the action you want a new version of the file I have the following suggestions:

  1. put the Summary and CSVWriter declarations inside the actionPerformed method just above the try. You are creating and closing it inside the method - why keep it around?

  2. put the writer close statement in a finally. Because if your thread is interrupted for any reason your file will not be closed (giving you an empty file).

  3. Use some sort of join logic after your threads are created. Otherwise you will still have threads processing when you call the close and you will lose data (possibly giving you an empty file).

Scott Conway
  • 975
  • 7
  • 13
  • Yep, already came up to that myself, but either way: open/close in the method, finally close and joining threads made it work. Thanks anyway! – Canert Sep 15 '17 at 21:40