每次附加到 CSV 文件时的不同编码
Different encodings every time I append to a CSV file
我以这种方式将 Windows-1252 中编码的文本附加到 CSV 中:
public static final Charset CHARSET = Charset.forName("Windows-1252");
public void dumpToCSV(final List<String[]> content,
final char delimiter,
final String enc,
final int csvDays) {
File file = new File(Constants.CSV_FILENAME);
// Convert the Character Format before dumping to file:
try (
OutputStreamWriter os = new OutputStreamWriter(
new FileOutputStream(file, true),
CHARSET);
CSVWriter cw = new CSVWriter(os, delimiter)) {
// Remove old lines
clearCsvByDays(file, csvDays, Character.toString(delimiter));
// Dump new content into file.
cw.writeAll(content);
} catch (IOException e) {}
}
private void clearCsvByDays(final File file, final int csvDays, final String delim)
throws IOException {
List<String> out = Files.lines(file.toPath(), CHARSET)
.filter(line -> mustFilter(line, csvDays, delim))
.collect(Collectors.toList());
Files.write(file.toPath(), out,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
}
第一次写入文件,结果符合预期,字符为Windows-1252编码,在目标程序上显示良好。
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- This result is fine.
第二个转储,它以UTF-8附加新数据,我不知道为什么。
"Éspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 2nd dump (new)
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 1st dump (old)
第三次转储,它将新数据附加到另一种不同的编码上,但保留第一个正确的行转储到 Windows-1252。
"Ãspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 3rd dump (new)
"Éspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 2nd dump (old)
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 1st dump (old)
如果我一直追加,每次都是不同的编码。
为什么会发生这种情况,我该如何解决?
CSVWriter 已获得正确的 OutputStreamWriter。
并且在写作时,Files.write 也需要编码。
Files.write(file.toPath(), out, CHARSET,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
所以我怀疑其他地方有黑客攻击:
new String(string.getBytes(...), ...)
我以这种方式将 Windows-1252 中编码的文本附加到 CSV 中:
public static final Charset CHARSET = Charset.forName("Windows-1252");
public void dumpToCSV(final List<String[]> content,
final char delimiter,
final String enc,
final int csvDays) {
File file = new File(Constants.CSV_FILENAME);
// Convert the Character Format before dumping to file:
try (
OutputStreamWriter os = new OutputStreamWriter(
new FileOutputStream(file, true),
CHARSET);
CSVWriter cw = new CSVWriter(os, delimiter)) {
// Remove old lines
clearCsvByDays(file, csvDays, Character.toString(delimiter));
// Dump new content into file.
cw.writeAll(content);
} catch (IOException e) {}
}
private void clearCsvByDays(final File file, final int csvDays, final String delim)
throws IOException {
List<String> out = Files.lines(file.toPath(), CHARSET)
.filter(line -> mustFilter(line, csvDays, delim))
.collect(Collectors.toList());
Files.write(file.toPath(), out,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
}
第一次写入文件,结果符合预期,字符为Windows-1252编码,在目标程序上显示良好。
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- This result is fine.
第二个转储,它以UTF-8附加新数据,我不知道为什么。
"Éspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 2nd dump (new)
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 1st dump (old)
第三次转储,它将新数据附加到另一种不同的编码上,但保留第一个正确的行转储到 Windows-1252。
"Ãspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 3rd dump (new)
"Éspáñà tëst";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 2nd dump (old)
"ʳpⲠ t촴";"ADN";"26-09-2017";"0";"0";"0";"0" <-- 1st dump (old)
如果我一直追加,每次都是不同的编码。
为什么会发生这种情况,我该如何解决?
CSVWriter 已获得正确的 OutputStreamWriter。
并且在写作时,Files.write 也需要编码。
Files.write(file.toPath(), out, CHARSET,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
所以我怀疑其他地方有黑客攻击:
new String(string.getBytes(...), ...)