Java 日看一类(23)之IO包中的FileWriter类

FileWriter继承自OutputStreamWriter

无任何引入包



类头注释如下:

/**
 * Convenience class for writing character files.  The constructors of this
 * class assume that the default character encoding and the default byte-buffer
 * size are acceptable.  To specify these values yourself, construct an
 * OutputStreamWriter on a FileOutputStream.
 *
 * <p>Whether or not a file is available or may be created depends upon the
 * underlying platform.  Some platforms, in particular, allow a file to be
 * opened for writing by only one <tt>FileWriter</tt> (or other file-writing
 * object) at a time.  In such situations the constructors in this class
 * will fail if the file involved is already open.
 *
 * <p><code>FileWriter</code> is meant for writing streams of characters.
 * For writing streams of raw bytes, consider using a
 * <code>FileOutputStream</code>.
 *
 * @see OutputStreamWriter
 * @see FileOutputStream
 *
 * @author      Mark Reinhold
 * @since       JDK1.1
 */

大意如下:

用于写出字符文件的方便类

这个类的构造函数一般使用默认的字符编码方式,和默认的byte缓冲大小

如果要自己设置这些值,请先创建一个OutputStreamWriter在FileOutoutStream之中

一个文件是否有效或者能否被创建取决于底层系统

尤其是一些底层系统,只允许一个文件在同一时间只能被一个FileWriter(或其他写入类)以写入方式打开

在一些情况下,当指定文件已经被打开后该类中的构造函数会执行失败(无法成功创建

FileWriter表示一个字符输出流

如需要原始byte输出流,请使用FileOutputStream




该类含有如下的成员方法:

构造函数(将写出文件的路径传入上层构造函数

public FileWriter(String fileName) throws IOException {
    super(new FileOutputStream(fileName));
}

构造函数(写出文件路径传入上层构造函数,同时附加文件是否为附加打开的属性

public FileWriter(String fileName, boolean append) throws IOException {
    super(new FileOutputStream(fileName, append));
}

构造函数(直接用File文件作为参数

public FileWriter(File file) throws IOException {
    super(new FileOutputStream(file));
}

构造函数(File+附加打开属性

public FileWriter(File file, boolean append) throws IOException {
    super(new FileOutputStream(file, append));
}

构造函数(文件描述符

public FileWriter(FileDescriptor fd) {
    super(new FileOutputStream(fd));
}




该类和FileReader一样,都仅存构造函数而通过继承方式来完成字节流的转化,如果想读懂这两类,请直接去看他们的上级类(OuputStreamWriter)或者查看该类的另一种形式FileOutputStream


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值