0

I'm trying to use Java FileWriter (and BufferedWriter) to write content to a csv file. This is my code:

import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;

import java.io.*;
import java.nio.file.Path;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UIDFaker {
    private final static Logger logger = Logger.getLogger(UIDFaker.class.getName());
    private FakeValuesService fakeValuesService = new FakeValuesService(new Locale("en-GB"), new RandomService());
    private static int MAX_NB_OF_ROWS;
    private String[] linesToWrite;

    public void writeToFile(int maxNbOfRows, String fileName) {
        MAX_NB_OF_ROWS = maxNbOfRows;
        linesToWrite = new String[MAX_NB_OF_ROWS + 1];
        initiateCSVFileHeader();
        initiateFakeData();
        writeToFile(fileName);
    }

    private void writeToFile(String fileName) {
        try {
            FileWriter fileWriter = new FileWriter(fileName);
            Writer bufferedWriter = new BufferedWriter(fileWriter);
            fileWriter.append(linesToWrite[0], 0, 13);
            for (int i = 1; i < MAX_NB_OF_ROWS + 1; i++) {
                fileWriter.write(linesToWrite[i], 0, 25);
            }


            //fileWriter.close();
            //bufferedWriter.close();
        }
        catch (IOException exception) {
            logger.info(exception.getMessage());
        }
    }

    private void initiateCSVFileHeader() {
        linesToWrite[0] = "\"RANK\";\"UID\"\n";
    }

    private void initiateFakeData() {
        String rank = "";
        String uid = "";
        String line = "";
        for (int i = 1; i < MAX_NB_OF_ROWS + 1; i++) {
            rank = generateFakeRank();
            uid = generateFakeUID();
            line = "\"" + rank + "\";\"" + uid + "\"\n";
            linesToWrite[i] = line;
        }
    }

    public String generateFakeRank () {
        return fakeValuesService.regexify("[0-9]{5}");
    }

    public String generateFakeUID() {
        return fakeValuesService.regexify("[0-9]{3}([A-Z][0-9]){4}[0-9]{3}");
    }

}

And when I opened the csv file, some of the characters didn't appear at the end of the file. I'm supposed to write 1001 lines including headers to the file (I set MAX_NB_OF_ROWS equals to 1000) but the file only showed me 984 lines, as in this picture: my csv file

Does anybody know how to fix it? Thank you very much

  • You forgot to close the buffered writer, so not all data is flushed to disk. And in the commented out code, you are closing in the wrong order. – Mark Rotteveel Jul 02 '20 at 13:03
  • He isn't using the `BufferedWriter` so closing (and thus flushing) won't do much. `flush` and `close` the `fileWriter` **or** use the `BufferedWriter` and close it (which should also flush the buffer). – M. Deinum Jul 02 '20 at 13:05
  • thank you, I changed the close order and it worked – Hải Trung Phạm Jul 02 '20 at 15:14

0 Answers0