All working, able to be built to .jar
This commit is contained in:
38
src/main/java/CellFileWriter.java
Normal file
38
src/main/java/CellFileWriter.java
Normal file
@@ -0,0 +1,38 @@
|
||||
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 CellFileWriter {
|
||||
|
||||
private String[] headers = {"Alpha", "Beta"};
|
||||
List<Integer[]> cells;
|
||||
String filename;
|
||||
|
||||
public CellFileWriter(String filename, CellSample cells) {
|
||||
if(!filename.matches(".*\\.csv")){
|
||||
filename = filename + ".csv";
|
||||
}
|
||||
this.filename = filename;
|
||||
this.cells = cells.getCells();
|
||||
}
|
||||
|
||||
public void writeCellsToFile() {
|
||||
CSVFormat cellFileFormat = CSVFormat.Builder.create()
|
||||
.setHeader(headers)
|
||||
.build();
|
||||
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW);
|
||||
CSVPrinter printer = new CSVPrinter(writer, cellFileFormat);
|
||||
){
|
||||
printer.printRecords(cells);
|
||||
} catch(IOException ex){
|
||||
System.out.println("Could not make new file named "+filename);
|
||||
System.err.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user