Java IO (2) - OutputStream

前言

JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包。Java 流在处理上分为字符流和字节流。字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符、字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组。

Java 内用 Unicode 编码存储字符,字符流处理类负责将外部的其他编码的字符流和 java 内 Unicode 字符流之间的转换。而类 InputStreamReader 和 OutputStreamWriter 处理字符流和字节流的转换。字符流(一次可以处理一个缓冲区)一次操作比字节流(一次一个字节)效率高。

0. 目录

  1. OutputStream
  2. FileOutputStream

1. OutputStream



OutputStream与InputStream对应,也是一个抽象类,其主要方法如下:

void	close()
Closes this output stream and releases any system resources associated
with this stream. void flush()
Flushes this output stream and forces any buffered output bytes to be written out. void write(byte[] b)
Writes b.length bytes from the specified byte array to this output stream.
void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off
to this output stream. abstract void write(int b)
Writes the specified byte to this output stream.

主要方法就是write方法,注意,只write字节。

2. FileOutputStream

FileOutputStream是OutputStream的一个子类,实现对文件的写入。

A file output stream is an output stream for writing data to a File or to a FileDescriptor. 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 FileOutputStream (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.

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

简单翻译

FileOutputStream是用来写文件的,但这个很依赖操作系统,有的操作系统只能操作一个FileOutputStream。FileOutputStream是用来写二进制的,如果要写字符,请使用FileWriter。

2.1. 构造函数

FileOutputStream(File file)
Creates a file output stream to write to the file represented by the
specified File object. FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the
specified File object. FileOutputStream(FileDescriptor fdObj)
Creates a file output stream to write to the specified file descriptor,
which represents an existing connection to an actual file in the file system. FileOutputStream(String name)
Creates a file output stream to write to the file with the specified name. FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.

如上,有五个构造函数,如果append为true,则追加写文件

FileOutputStream fos = new FileOutputStream("d:/123.txt", true);

2.2. 主要方法

void	close()
Closes this file output stream and releases any system resources associated
with this stream. protected void finalize()
Cleans up the connection to the file, and ensures that the close method of
this file output stream is called when there are no more references to this stream. FileChannel getChannel()
Returns the unique FileChannel object associated with this file output stream. FileDescriptor getFD()
Returns the file descriptor associated with this stream. void write(byte[] b)
Writes b.length bytes from the specified byte array to this file output stream. void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to
this file output stream. void write(int b)
Writes the specified byte to this file output stream.

2.4. 示例代码

	private void test() {
try {
FileOutputStream fos = new FileOutputStream("d:/123.txt", true);
String s = "礼拜天";
fos.write(s.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

注意:流要close;不处理乱码。为什么不处理乱码呢?因为这个是写二进制的,二进制可没有编码的问题。

Java IO (2) - OutputStream的更多相关文章

  1. Java IO 之 OutputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  2. java IO文件读写例子(OutputStream,InputStream,Writer,Reader)

    一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...

  3. Java IO 过滤流 BufferedInput/OutputStream

    Java IO 过滤流 BufferedInput/OutputStream @author ixenos 概念 BufferedInput/OutputStream是实现缓存的过滤流,他们分别是Fi ...

  4. Java IO 节点流 FileInput/OutputStream

    Java IO 节点流 FileInput/OutputStream @author ixenos 节点流之 文件流 文件读写是最常见的I/O操作,通过文件流来连接磁盘文件,读写文件内容 1.文件的读 ...

  5. Java IO 节点流 ByteArrayInput/OutputStream

    Java IO 节点流 ByteArrayInput/OutputStream @author ixenos ByteArrayInputStream 包含一个内部缓冲区(字节数组byte[]),该缓 ...

  6. 【java】io流之字节输出流:java.io.OutputStream类及子类java.io.FileOutputStream

    package 文件操作; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...

  7. Java IO流操作汇总: inputStream 和 outputStream【转】

    我们在进行Android java 开发的时候,经常会遇到各种IO流操作.IO流操作一般分为两类:字符流和字节流.以“Reader”结尾都是字符流,操作的都是字符型的数据:以“Stream”结尾的都是 ...

  8. java io系列03之 ByteArrayOutputStream的简介,源码分析和示例(包括OutputStream)

    前面学习ByteArrayInputStream,了解了“输入流”.接下来,我们学习与ByteArrayInputStream相对应的输出流,即ByteArrayOutputStream.本章,我们会 ...

  9. java.io.OutputStream & java.io.FileOutputStream

    java.io.OutputStream & java.io.FileOutputStream 1.Java.io.OutputStream(字节输出流) 字节输出流,这是一个抽象类,是表示输 ...

随机推荐

  1. Oracle 多实例如何通过EM进行访问-portlist.ini

    [root@redhat4 install]# pwd/u01/app/oracle/product/11.2.0/dbhome_1/install[root@redhat4 install]# mo ...

  2. Windows下搭建MySql Master-Master Replication

    1.首先下载最新版的MySql Server (http://dev.mysql.com/downloads/windows/installer/) 2.安装MySql Server到两台机器上 My ...

  3. bzoj1312

    忘写题解了,经典的最大密度子图 可以类似分数规划的做,二分密度,然后转化为最大权闭合子图做,判断是否大于0 注意方案的输出 const eps=1e-6; lim=1e-12; inf=; type ...

  4. 【转】如何在eclipse里关联查看android源码

    原文网址:http://fengbohaishang.blog.51cto.com/5106297/1339556 以前没怎么注意过这个问题,不怎么看源码,现在发现源码确实是很好的学习资源. 直入正题 ...

  5. php mysql事务

    这里记录一下php操作mysql事务的一些知识 要知道,MySQL默认的行为是在每条SQL语句执行后执行一个COMMIT语句,从而有效的将每条语句独立为一个事务.但是,在使用事务时,是需要执行多条sq ...

  6. C#中的lock关键字

    前几天与同事激烈讨论了一下,有一点收获,记录起来. 首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. ...

  7. ruby函数回调的实现方法

    以前一直困惑ruby不像python,c可以将函数随意传递,然后在需要的时候才去执行.其实本质原因是ruby的函数不是对象. 通过查阅资料发现可以使用如下方法: def func(a, b) puts ...

  8. [转]python类方法

    Python定义类-方法 公有方法.私有方法.类方法.静态方法

  9. CF 577C Vasya and Petya's Game

    题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...

  10. Jedis的JedisSentinelPool源代码分析

    概述 Jedis是Redis官方推荐的Java客户端,更多Redis的客户端可以参考Redis官网客户端列表.Redis-Sentinel作为官方推荐的HA解决方案,Jedis也在客户端角度实现了对S ...