0. 概述:
Java中基于流的I/O构建在4个抽象类之上, 其中2个是字节流,另外2个是字符流。
字节流:
InputStream / OutputStream
当操作字节或其它二进制对象时,应当使用字节流。
字符流:
Reader / Writer
当操作字符或字符串时,应当使用字符流。
1. InputStream
InputStream: 输入字节流,它是一个抽象类。
常用主要方法:
int read()
返回代表下一个可用字节的整数,当文件达到末尾时,返回-1。
int read(bute buffer[])
尝试读取buffer.length个字节到buffer中,返回实际成功读取的字节数;文件到末尾返回-1。
int read(bute buffer[], int offset, int numBytes)
尝试读取numBytes个字节到buffer中,从buffer[offset]开始保存读取的字节;
返回实际成功读取的字节数;文件到末尾返回-1。
2. OutputStream
OutputStream: 输出字节流,它是一个抽象类。
OutputStream有很多子类,它们分别负责向不同的介质写入数据。
例如:
FileOutputStream 写入数据到文件
TelnetOutputStream 写入数据到网络连接
ByteArrayOutputStream 写入数据到字节数组
常用主要方法:
void write(int b)
向输出流中写入单个字节。
void write(byte buffer[])
向输出流中写入一个完整的字节数组。
void write(byte buffer[], int offset, int numBytes)
从buffer[offset]开始将numBytes个字节写入输出流中。
void flush()
结束输出状态,刷新输出缓冲区。
3. FileInputStream
文件读取字节流。
常用构造函数:
FileInputStream(String filePath) (更常用)
FileInputStream(File fileObj)
一个同构输入流来读取文件内容的例子,演示了3种方式(单字节读取, 字节数组读取, 忽略部分字节读取)
代码如下:
package corejava8.io.stream; import java.io.*; public class FileInputStreamDemo {
public static void main(String args[]) {
int size; // Use try-with-resources to close the stream
try (FileInputStream f = new FileInputStream("src/corejava8/io/stream/FileInputStreamDemo.java")) {
System.out.println("Total Available Bytes: " + (size = f.available()));
int n = size / 40; // 通过循环每次单字节读取
System.out.println("First " + n + " bytes of the file one read() at a time");
for (int i = 0; i < n; i++) {
System.out.print((char) f.read());
}
System.out.println("\nStill Available: " + f.available());
System.out.println("--------------------------------------------------------------"); // 字节数组读取
System.out.println("Reading the next " + n + " with one read(b[])");
byte b[] = new byte[n];
if (f.read(b) != n) {
System.err.println("couldn’t read " + n + " bytes.");
}
System.out.println(new String(b, 0, n));
System.out.println("\nStill Available: " + (size = f.available()));
System.out.println("--------------------------------------------------------------"); // 忽略部分字节读取
System.out.println("Skipping half of remaining bytes with skip()");
f.skip(size / 2);
System.out.println("Still Available: " + f.available());
System.out.println("Reading " + n / 2 + " into the end of array");
if (f.read(b, n / 2, n / 2) != n / 2) {
System.err.println("couldn't read " + n / 2 + " bytes.");
}
System.out.println(new String(b, 0, b.length));
System.out.println("\nStill Available: " + f.available());
} catch (IOException e) {
System.out.println("I/O Error: " + e);
}
}
}
运行结果如下:
Total Available Bytes: 1704
First 42 bytes of the file one read() at a time
package corejava8.io.stream;
import java.
Still Available: 1662
--------------------------------------------------------------
Reading the next 42 with one read(b[])
io.*;
public class FileInputStreamDemo {
Still Available: 1620
--------------------------------------------------------------
Skipping half of remaining bytes with skip()
Still Available: 810
Reading 21 into the end of array
io.*;
public class Fm.err.println("couldn
Still Available: 789
4. FileOutputStream
文件输出字节流,用于向文件中写入字节。
4个常用构造函数:
FileOutputStream(String filePath)
FileOutputStream(File fileObj)
FileOutputStream(String filePath, boolean append)
FileOutputStream(File fileObj, boolean append)
在开始下面的例子前,我们先提到流关闭的一个细节,那就是
在JDK 7之前的遗留代码,都使用显式地调用close()方法来关闭流,这种办法
比较笨拙。
老的代码:
try {
FileOutputStream f0 = new FileOutputStream("file1.txt");
// 文件写入操作
} catch(IOException e) {
System.out.println("An I/O Error Occurred");
} finally {
try {
if(f0 != null) f0.close();
} catch(IOException e) {
System.out.println("Error Closing file1.txt");
}
}
}
新的代码(JDK 7及以后的代码):
带资源的try(try with resources)
try (FileOutputStream f0 = new FileOutputStream("file1.txt")) {
// 文件写入操作
} catch(IOException e) {
System.out.println("An I/O Error Occurred");
}
是不是代码变得更加简洁了?
我们的例子:
package corejava8.io.stream; import java.io.*; public class FileOutputStreamDemo {
public static void main(String args[]) {
String source = "Now is the time for all good men\n" + " to come to the aid of their country\n"
+ " and pay their due taxes.";
byte buf[] = source.getBytes();
// Use try-with-resources to close the files.
try (FileOutputStream f0 = new FileOutputStream("file1.txt");
FileOutputStream f1 = new FileOutputStream("file2.txt");
FileOutputStream f2 = new FileOutputStream("file3.txt")) {
// write to first file
for (int i = 0; i < buf.length; i++)
f0.write(buf[i]); // write to second file
f1.write(buf); // write to third file
f2.write(buf, 3, buf.length - 3);
} catch (IOException e) {
System.out.println("An I/O Error Occurred");
}
}
}
运行结果如下:
第一个,第二个文件内容都是:
Now is the time for all good men
to come to the aid of their country
and pay their due taxes.
第三个文件跳过第一个单词,内容如下:
is the time for all good men
to come to the aid of their country
and pay their due taxes.
上述代码中:
byte buf[] = source.getBytes();
用于获取源数据(字符串)的字节数组。