首页 技术 正文
技术 2022年11月17日
0 收藏 458 点赞 4,980 浏览 1166 个字

首先看JAVA API的描述:

ABOUT OutputStreamWriter:

“An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform’s default charset may be accepted.”

「OutputStreamWriter是连接字符流和字节流的桥梁:写入这个类的字符流会被编码为“charset”所指示的编码格式。这个charset可以通过名字指定或是直接给出,也接受平台的默认charset(好像是UTF-8)。」

ABOUT FileWriter:

“The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.”

「这个类的构造函数假定用户使用默认的编码方式,并且允许使用默认的字节缓冲大小。」

另外FileWriter是OutputStreamWriter的子类。FileWriter只能使用默认的编码UTF-8,这造成输出中文的时候会出现乱码。而OutputStreamWriter可以避免这一点。

使用:

OutputStreamWriter(OutputStream out, Charset cs)
Creates an OutputStreamWriter that uses the given
charset.

当然还有其他参数的OutputStreamWriter类,在API中有列举。

于是可以写成:

OutputStreamWriter ow = new OutputStreamWriter(os,Charset.forName("unicode"));

其中os是OutputStream类的实例。但是Eclipse会提示OutputStream是不能直接实例化的,这时可以通过FileOutputStream向上转型(关于对象的转型)来得到:

OutputStream os = null ;
os = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/Android/ContactME.txt");

以上。

参考:http://zhidao.baidu.com/question/918312230716365579.html?quesup2&oldq=1

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851