How to remove ^ M characters from a text file regardless of platform using Java?

I am trying to figure out how to remove ^ M characters from a text file that is generated from Java code using the following code.

   public StringBuilder toCsv(Table table) {
        StringBuilder stringBuilder = new StringBuilder();
        String csv = new String();
        for (Column cName : table.getColumns()){
            csv += QUOT;
            csv += cName.getName();
            csv += QUOT;
            csv += CSV_SEPERATOR;
        }

        csv += "\n";
        stringBuilder.append(csv);

        for (Row row : table) {
            Collection<Object> values = row.values();
            String csvString = "";
            if (values.size() == 10) {
                String ep = QUOT + CSV_SEPERATOR + QUOT ;
                csvString = StringUtils.join(row.values(),ep );
                csvString.replaceAll("\'", "");
                csvString = QUOT + csvString + QUOT;
                logger.info("line ++++ " + csvString);
            }
            stringBuilder.append(csvString);
            stringBuilder.append("\n");
        }

        return stringBuilder;
    } 

then I use the following method to write data to a file

 public void writeCsv(String data, String path, String fileName) throws IOException {
    String completePath = path + "/" + fileName;
    Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream(completePath)));
    try {
        out.write(data);
    } finally {
        out.close();
    }
}

Context

I am creating CSV files using http://jackcess.sourceforge.net/ from a Microsoft access file (.mdb). When I create csv and open with vim, I see a lot ^ M in the middle of the lines. NOTE. I am on macOS

I tried following to remove ^ M (which, I believe, MS Windows CARRIAGE_RETURN) before writing in csv

 csvLine.replaceAll("\n\r", "");

and

 csvLine.replaceAll("\r\n", "");

and

csvLine.replaceAll("\\r", "");

Generated CSV

'10773.0';'';'';'';'Thu Jul 14 00:00:00 CEST 2016';'By Cash';'';'10000.0';'';'2102.0'
    '10001.0';'';'';'';'Thu Jul 14 00:00:00 CEST 2016';'Pet Soup cash';'087470^M
        ^M
        ^M
087470';'-45000.0';'';'2102.0'
'10360.0';'';'';'';'Thu Jul 14 00:00:00 CEST 2016';'By Cash';'';'37000.0';'';'2101.0'
'10444.0';'';'';'';'Thu Jul 14 00:00:00 CEST 2016';'By Cash';'';'2000.0';'';'2101.0'

As you can see, one line above the CSV is split into ^ M, which is undesirable. I need to programmatically remove such characters from a file.

^ M

'10001.0';'';'';'';'Thu Jul 14 00:00:00 CEST 2016';'Pet Soup cash';'087470087470';'-45000.0';'';'2102.0'

.

+4
1

, .replaceAll ; String. ,

String csvString = "123,foo,234";
csvString.replaceAll("foo", "");
System.out.println(csvString);

123,foo,234

, . ,

String csvString = "123,foo,234";
csvString = csvString.replaceAll("foo", "");  // save the new value
System.out.println(csvString);

123,,234

, ,

csvString = csvString.replaceAll("\r\n", "");  // save the new value

frame_return ( ^M), new_line ( ).

+3

Source: https://habr.com/ru/post/1665508/


All Articles