All working, able to be built to .jar
This commit is contained in:
52
src/main/java/MatchingFileWriter.java
Normal file
52
src/main/java/MatchingFileWriter.java
Normal file
@@ -0,0 +1,52 @@
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVPrinter;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class MatchingFileWriter {
|
||||
|
||||
private String filename;
|
||||
private List<String> comments;
|
||||
private List<String> headers;
|
||||
private List<List<String>> results;
|
||||
|
||||
public MatchingFileWriter(String filename, List<String> comments, List<String> headers, List<List<String>> results){
|
||||
if(!filename.matches(".*\\.csv")){
|
||||
filename = filename + ".csv";
|
||||
}
|
||||
this.filename = filename;
|
||||
this.comments = comments;
|
||||
this.headers = headers;
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
public void writeResultsToFile(){
|
||||
String[] headerStrings = new String[headers.size()];
|
||||
for(int i = 0; i < headers.size(); i++){
|
||||
headerStrings[i] = headers.get(i);
|
||||
}
|
||||
CSVFormat resultsFileFormat = CSVFormat.Builder.create()
|
||||
.setCommentMarker('#')
|
||||
//.setHeader(headerStrings)
|
||||
.build();
|
||||
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
||||
CSVPrinter printer = new CSVPrinter(writer, resultsFileFormat);
|
||||
){
|
||||
for(String comment: comments){
|
||||
printer.printComment(comment);
|
||||
}
|
||||
results.add(0, headers);
|
||||
printer.printRecords(results);
|
||||
|
||||
} catch(IOException ex){
|
||||
System.out.println("Could not make new file named "+filename);
|
||||
System.err.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user