CSVWriter.java 796 B

123456789101112131415161718192021222324252627282930313233343536
  1. package csv;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. public class CSVWriter {
  5. FileWriter writer;
  6. char separator;
  7. boolean newline = true;
  8. public CSVWriter(String filename, char separator) throws IOException {
  9. this.writer = new FileWriter(filename);
  10. this.separator = separator;
  11. }
  12. public void write(String record) throws IOException {
  13. if (!newline) {
  14. this.writer.append(this.separator);
  15. } else {
  16. this.newline = false;
  17. }
  18. this.writer.append(record);
  19. }
  20. public void writeln() throws IOException {
  21. this.writer.append('\n');
  22. this.newline = true;
  23. }
  24. public void close() throws IOException {
  25. this.writer.flush();
  26. this.writer.close();
  27. }
  28. }