FileOutputStream使用指定的编码将字节写入文件

huangapple go评论69阅读模式
英文:

FileOutputStream set encoding with write byte to file

问题

我有一个CSV文件我以UTF-8编码保存它如下所示

FileOutputStream fileOut = new FileOutputStream(file, true);

/*UTF-8设置*/
fileOut.write(0xef);
fileOut.write(0xbb);
fileOut.write(0xbf);

/*追加另一个字符串*/
PrintWriter printWriter = new PrintWriter(fileOut);
printWriter.write(...);

我将字符串写入文件并且使用`UTF-8`编码可以正常工作

我的问题是
1. 有人知道这些字节是什么吗
2. 如何将其更改为另一种编码比如`cp1256`?
3. 有没有提供所有编码的列表

**更新**
假设我保存了分页创建的大量字符串数据我无法一次性存储这么大的数据量

谢谢
英文:

i have a csv file that i save it with utf-8 encoding like below:
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(...);

i write string to file and it work correctly with UTF-8 encodeing.

my question is :

  1. anyone know what is this bytes?
  2. how can change it to another encoding like cp1256?
  3. is a list that provide all encoding for that?

UPDATE:
assume that i save huge string data that create in pagination and i can not store this size of data in one time.

thanks

答案1

得分: 2

为什么不使用来自Apache Commons的FileUtils呢?
这会让你的生活变得更加轻松。 FileOutputStream使用指定的编码将字节写入文件

而且,如果你想转换内容,可以在这里查看:Java中的编码转换

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

Why you don't use the FileUtils from Apache Commons?
This will make your life much easier. FileOutputStream使用指定的编码将字节写入文件

And, If you want to covert the content, have a look here: Encoding conversion in java

&lt;!-- https://mvnrepository.com/artifact/commons-io/commons-io --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;commons-io&lt;/groupId&gt;
    &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
    &lt;version&gt;2.8.0&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年9月23日 15:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64023316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定