英文:
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 :
- anyone know what is this bytes?
- how can change it to another encoding like
cp1256
? - 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呢?
这会让你的生活变得更加轻松。
而且,如果你想转换内容,可以在这里查看: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.
And, If you want to covert the content, have a look here: 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论