Advertisement
Guest User

as

a guest
Mar 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. //Klasa odpowiedzialna za zapisanie
  2.  
  3. package Implementacja;
  4.  
  5. import java.io.IOException;
  6. import java.io.Writer;
  7. import java.util.List;
  8.  
  9. public class CSVUtils
  10. {
  11.  
  12. private static final char DEFAULT_SEPARATOR = ',';
  13.  
  14. public static void writeLine(Writer w, List<String> values) throws IOException
  15. {
  16. writeLine(w, values, DEFAULT_SEPARATOR, ' ');
  17. }
  18.  
  19. public static void writeLine(Writer w, List<String> values, char separators) throws IOException
  20. {
  21. writeLine(w, values, separators, ' ');
  22. }
  23.  
  24. // https://tools.ietf.org/html/rfc4180
  25. private static String followCVSformat(String value)
  26. {
  27.  
  28. String result = value;
  29. if (result.contains("\""))
  30. {
  31. result = result.replace("\"", "\"\"");
  32. }
  33. return result;
  34.  
  35. }
  36.  
  37. public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException
  38. {
  39.  
  40. boolean first = true;
  41.  
  42. // default customQuote is empty
  43.  
  44. if (separators == ' ')
  45. {
  46. separators = DEFAULT_SEPARATOR;
  47. }
  48.  
  49. StringBuilder sb = new StringBuilder();
  50. for (String value : values)
  51. {
  52. if (!first)
  53. {
  54. sb.append(separators);
  55. }
  56. if (customQuote == ' ')
  57. {
  58. sb.append(followCVSformat(value));
  59. } else
  60. {
  61. sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
  62. }
  63.  
  64. first = false;
  65. }
  66. sb.append("\n");
  67. w.append(sb.toString());
  68.  
  69. }
  70.  
  71. }
  72.  
  73. //Przykład zapisu
  74. static String csvFile = "assets/solutions_big/200_20_55_9.csv";
  75. FileWriter writer = new FileWriter(csvFile);
  76. //W pętli
  77. List<String> staty = GetStatsEv(schedules, generation);
  78. CSVUtils.writeLine(writer, staty);
  79.  
  80. //Po pętli
  81. writer.flush();
  82. writer.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement