总结
I/O流分类
- 按照操作单元划分,字节I/O系统和字符I/O系统。
- 按照流的流向分,可以将流分为输入流和输出流
所有超类
- java.io.InputStream
- java.io.OutputStream
- java.io.Reader
- java.io.Writer
常用流类结构
流特点
带Buffered的性能高,使用了缓存,将数据保存在内存中,一定量时再进行写入磁盘,提高了效率。
字节流 InputStream OutputStream
FileInputStream/FileOutputStream
public static void writeFile() {
FileOutputStream fos = null;
char a = 'A';
char b = '中';
String s = "Hello 中国";
try {
fos = new FileOutputStream("data1.txt");
fos.write(a);
// fos.write(b); // 中文占两个字节,虽然write接收int,但实际只输出低位一个字节,故该句会引起乱码
fos.write(String.valueOf(b).getBytes());
fos.write(s.getBytes()); // Project Encoding为UTF-8,故以utf-8编码解码字节 data1.txt根据字节特征识别为utf-8格式,故不会乱码
fos.write(s.getBytes("gbk")); // 以gbk编码解码字节,由于以包含utf-8字节,再放入gbk编码字节,data1.txt无法识别编码,故会乱码
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readFile() {
FileInputStream fis = null;
try {
// 可以指定全路径,也可以相对路径(相对于工程根目录)
fis = new FileInputStream("data.txt");
byte[] bytes = new byte[10];
// 读取出来的就是硬盘上该文件内容的字节序列
int v = fis.read(bytes);
while (v != -1) {
// 输出到控制台,默认以ANSI编码解析,如果Studio控制台指定了编码格式,则以对应编码格式解析
System.out.write(bytes, 0, v);
v = fis.read(bytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
DataInputStream/DataOutputSrream
public static void binaryRead() {
FileInputStream fis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream("data.txt");
dis = new DataInputStream(fis);
// 读取顺序必须完全一致
byte a = dis.readByte();
boolean flag = dis.readBoolean();
int b = dis.readInt();
float f = dis.readFloat();
String str = dis.readUTF(); // 通过UTF-8编码解析基础输入流的字节
System.out.println(a);
System.out.println(flag);
System.out.println(b);
System.out.println(f);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void binaryWrite() {
byte a = 1;
boolean flag = true;
int b = 10;
float f = 30.12f;
String str = "Hello 中国";
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fos = new FileOutputStream("data.txt");
dos = new DataOutputStream(fos);
dos.writeByte(a);
dos.writeBoolean(flag);
dos.writeInt(b);
dos.writeFloat(f);
dos.writeUTF(str); // 使用 UTF-8编码将一个字符串写入基础输出流
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dos != null) {
try {
// 只需关外层流
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
PipedInputStream/PipedOutputStream
public class Test {
public static void main(String[] args) {
/**
* 流程
* 1 建立输入输出流
* 2 绑定输入输出流
* 3 向缓冲区写数据
* 4 读取缓冲区数据
*/
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
try {
pos.connect(pis); // 接通输入和输出形成管道
Producer producer = new Producer(pos);
Consumer consumer = new Consumer(pis);
producer.start();
consumer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 生产者线程
*/
class Producer extends Thread {
// 输出流
private PipedOutputStream out = new PipedOutputStream();
// 构造方法
public Producer(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
writeMessage();
}
private void writeMessage() {
StringBuilder sb = new StringBuilder("Hello World!!!");
try {
out.write(sb.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 消费线程
*/
class Consumer extends Thread {
//输入流, 默认缓冲区大小为1024
private PipedInputStream in = new PipedInputStream();
//构造方法
public Consumer(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
readMessage();
}
private void readMessage() {
byte[] buf = new byte[1024];
try {
int len = in.read(buf);
System.out.println("缓冲区的内容为: " + new String(buf, 0, len));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流
InputStreamReader/OutputStreamWriter
private static void charRead() {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
fis = new FileInputStream("data.txt");
// 默认使用系统设定编码,也可指定编码,eg:使用utf-8编码解析,如果写入和读取编码不一致,很可能乱码
isr = new InputStreamReader(fis, "UTF-8");
char[] chars = new char[10];
int count = isr.read(chars);
while (count != -1) {
System.out.print(new String(chars, 0, count));
count = isr.read(chars);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void charWrite() {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream("data.txt");
osw = new OutputStreamWriter(fos);
osw.write("Hello 中");
osw.write('国');
} catch (IOException e) {
e.printStackTrace();
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader/BufferedWriter
public static void bufferReadWrite() {
// 读取用户从控制台的输入,并写入文件,直到用户输入了ok为止
String input = null;
BufferedReader br = null;
BufferedWriter bw = null;
String filePath = "data.txt";
br = new BufferedReader(new InputStreamReader(System.in));
try {
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
do {
System.out.print("请输入一行文字:");
try {
input = br.readLine();
bw.write(input);
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
} while (!input.equals("ok"));
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读取内容并显示
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
filePath)));
input = br.readLine();
while (input != null) {
System.out.println(input);
input = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out/System.in PrintStream/InputStream
out就是PrintStream类,其print方法都是通过BufferedWriter实现的。
网友评论