Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Klasa odpowiedzialna za zapisanie
- package Implementacja;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.List;
- public class CSVUtils
- {
- private static final char DEFAULT_SEPARATOR = ',';
- public static void writeLine(Writer w, List<String> values) throws IOException
- {
- writeLine(w, values, DEFAULT_SEPARATOR, ' ');
- }
- public static void writeLine(Writer w, List<String> values, char separators) throws IOException
- {
- writeLine(w, values, separators, ' ');
- }
- // https://tools.ietf.org/html/rfc4180
- private static String followCVSformat(String value)
- {
- String result = value;
- if (result.contains("\""))
- {
- result = result.replace("\"", "\"\"");
- }
- return result;
- }
- public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException
- {
- boolean first = true;
- // default customQuote is empty
- if (separators == ' ')
- {
- separators = DEFAULT_SEPARATOR;
- }
- StringBuilder sb = new StringBuilder();
- for (String value : values)
- {
- if (!first)
- {
- sb.append(separators);
- }
- if (customQuote == ' ')
- {
- sb.append(followCVSformat(value));
- } else
- {
- sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
- }
- first = false;
- }
- sb.append("\n");
- w.append(sb.toString());
- }
- }
- //Przykład zapisu
- static String csvFile = "assets/solutions_big/200_20_55_9.csv";
- FileWriter writer = new FileWriter(csvFile);
- //W pętli
- List<String> staty = GetStatsEv(schedules, generation);
- CSVUtils.writeLine(writer, staty);
- //Po pętli
- writer.flush();
- writer.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement