【问题标题】:FileOutputStream set encoding with write byte to fileFileOutputStream 设置编码,将字节写入文件
【发布时间】:2021-01-09 09:08:39
【问题描述】:

我有一个 csv 文件,我用 utf-8 编码保存它,如下所示: reference

FileOutputStream fileOut =fileOut = new FileOutputStream(file, true);
       
/*utf-8 setting*/
fileOut.write(0xef);
fileOut.write(0xbb);
fileOut.write(0xbf);

 /*append another string*/
 PrintWriter printWriter = new PrintWriter(fileOut);
 printWriter.write(...);
    

我将字符串写入文件,它与UTF-8 编码一起正常工作。

我的问题是:

  1. 有人知道这个字节是什么吗?
  2. 如何将其更改为另一种编码,例如cp1256?
  3. 是一个提供所有编码的列表吗?

更新: 假设我保存了在分页中创建的大量字符串数据,并且我无法一次存储这种大小的数据。

谢谢

【问题讨论】:

  • 这是一个 UTF-8 BOM,大自然最残酷的错误。你不能用它来改变编码。

标签: java fileoutputstream


【解决方案1】:

为什么不使用Apache Commons 的FileUtils? 这将使您的生活更轻松。 ;-)

而且,如果你想隐藏内容,请看这里:Encoding conversion in java

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
</dependency>

【讨论】:

  • 因为我有大量数据要保存在 csv 文件中,所以我需要在每个数据分页中保存它。所以 FileUtils.write(file,"data",Charset) 对我没有帮助。我编辑问题
  • 好的,我使用FileUtils.write 的第四个参数,它对我有帮助。谢谢我加上这个答案
猜你喜欢
  • 1970-01-01
  • 2015-08-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
相关资源
最近更新 更多