可见,输出方法可以输出单个字节和字节数组。
将字符串转换为字节数组可以使用String类的getBytes()方法。
除了写的几个重载方法,还有以下几个方法:
void flush():刷新此输出流并强制写出所有缓冲的输出字节。
void close():关闭此输出流并释放与此流有关的所有系统资源。
注意,即使没有使用flush和close方法,依然可以将数据输出。这与字符流不同。因为字符流带有默认的缓冲:字符由两个字节组成,读入或写出一个字符时,先要将第一个字节保存,在读到第二个字节时再一起查表,才可以变为一个完整的字符。所以要刷新默认的缓存。
1.2 FileOutputStream
FileOutputStream是用于写入诸如图像数据之类的原始字节的流。
构造器:
FileOutputStream(String fileName):创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String
fileName, boolean append):创建一个向具有指定name的文件中写入数据的输出文件流。如果第二个参数为true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream(File
file):创建一个向指定File对象表示的文件中写入数据的文件输出流。
FileOutputStream(File
file, boolean append):创建一个向指定File对象表示的文件中写入数据的文件输出流。如果第二个参数为true,则将字节写入文件末尾处,而不是写入文件开始处。
见下面程序(在使用输入输出类的方法中,有许多方法会抛出异常,这里为了方便学习与查看,没有将异常处理而是抛出,后面会有详细的异常处理)
public class OutputStreamDemo {
public static void main(String[] args) throws IOException {
//字节流
FileOutputStream fos = new FileOutputStream("demo.txt");
//写入单个字节
fos.write('a');
//写入字节数组,将字符串变为字节数组。
fos.write("你好".getBytes());
//关闭字节流
fos.close();
}
}
2.InputStream和FileInputStream
2.1 InputStream
InputStream是抽象类,表示字节输入流的所有类的超类。
用于输入的方法:
int read():从输入流中读取数据的下一个字节。返回0
到255
范围内的
int
字节值。如果因为已经到达流末尾而没有可用的字节,则返回值-1。输出时需要进行强转(byte)。如果读的是中文字符,那么会发生乱码。因为2个字节才是一个完整的字符。
int read(byte[] b):从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。返回读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回-1。
int read(byte[] b, int off, int len): 将输入流中最多len个数据字节读入 byte 数组。返回读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回-1。
其他方法:
int available():返回文件的字节数。
使用该方法,可以在起初就创建一个长度与被读取文件字节数相同的数组,这样输出时就不需要再循环。但是如果源文件过大,会发生内存溢出。
2.2 FileInputStream
FileInputStream用于读取诸如图像数据之类的原始字节流。
构造器:
FileInputStream(String fileName):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的路径名fileName指定。
FileInputStream(File
file):通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的File对象file指定。
public class InputStreamDemo {
public static void main(String[] args) throws IOException {
method_1();
method_2();
method_3();
}
//输入单个字节
public static void method_1() throws IOException{
FileInputStream fis = new FileInputStream("demo.txt");
int b = 0;
//判断是否读到末尾的只要判断返回值是否为-1
while((b = fis.read()) != -1){
//打印时需要进行强转
System.out.println((char)b);
}
}
//使用字节数组
public static void method_2() throws IOException{
FileInputStream fis = new FileInputStream("demo.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1){
System.out.println(new String(buf, 0 ,len));
}
}
//使用字节数组,大小为文件字节数
public static void method_3() throws IOException{
FileInputStream fis = new FileInputStream("demo.txt");
//大小等于文件字节数
byte[] buf = new byte[fis.available()];
//不需要使用循环
fis.read(buf);
System.out.println(new String(buf));
}
}
3.BufferedInputStream和BufferedOutputStream
BufferedInputStream和BufferedOutputStream是带有缓冲的字节流。
3.1 BufferedInputStream
构造器
BufferedInputStream(InputStream
in):创建一个BufferedInputStream并保存其参数,即输入流in,以便将来使用。
方法:
void close():关闭字节流。不需要手动关闭包装的字节流。
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
//字节流
FileInputStream fis = new FileInputStream("c:\\demo.txt");
//带有缓冲的字节流
BufferedInputStream bufin = new BufferedInputStream(fis);
//字节数组
byte[] buf = new byte[1024];
int len = 0;
while((len = bufin.read(buf)) != -1){
System.out.print(new String(buf, 0, len));
}
bufin.close();
}
}
3.2 BufferedOutputStream
构造器:
BufferedOutputStream(OutputStream out):创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
方法:
void
close():关闭字节流。不需要手动关闭包装的字节流。
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
//字节流
FileOutputStream fos = new FileOutputStream("c:\\demo.txt");
//带缓存的字节流
BufferedOutputStream bufout = new BufferedOutputStream(fos);
//输出字节数组,将字符串转换为字节数组
bufout.write("java".getBytes());
bufout.close();
}
}