文章目录
前言
一、字符流
数据流中最小的数据单元是字节。
Java 中的字符是 Unicode 编码,一个字符占用两个字节。所以字符流以两个字节(即 2 byte,16 bit)作为一个数据单元。字符流主要用于纯文本文件的读取和写入
1. CharArrayReader、CharArrayWriter
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
/**
* @author Mr.Guo
* @date 2021/2/8 下午5:25
*/
public class ChatIoTest {
public static void main(String[] args) {
CharArrayReader reader = new CharArrayReader("大家好,我是码农蝈蝈。".toCharArray());
CharArrayWriter writer = new CharArrayWriter();
try {
int c = -1;
while ((c = reader.read()) > -1) {
System.out.println((char) c);
writer.write(c);
}
System.out.println(new String(writer.toCharArray()));
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
writer.close();
}
}
}
2. StringReader、StringWriter
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
/**
* @author Mr.Guo
* @date 2021/2/8 下午4:41
*/
public class IoTest {
public static void main(String[] args) throws IOException {
StringReader stringReader = new StringReader("大家好,我是码农蝈蝈。");
StringWriter stringWriter = new StringWriter();
try {
int c = -1;
while ((c = stringReader.read()) > -1) {
System.out.println((char) c);
stringWriter.write(c);
}
System.out.println(stringWriter.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
stringReader.close();
stringWriter.close();
}
}
}
3. InputStreamReader、OutputStreamWriter
import java.io.*;
/**
* @author Mr.Guo
* @date 2021/2/9 上午8:32
*/
public class FileIoTest {
public static void main(String[] args) {
InputStreamReader reader = null;
OutputStreamWriter writer = null;
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
File file;
inputStream = new FileInputStream("/work/test.txt");
reader = new InputStreamReader(inputStream);
outputStream = new FileOutputStream("/work/test_.txt");
writer = new OutputStreamWriter(outputStream);
int c = -1;
while ((c = reader.read()) > -1) {
writer.write(c);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != inputStream) {
inputStream.close();
}
if (null != reader) {
reader.close();
}
if (null != outputStream) {
outputStream.close();
}
if (null != writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4. FileReader、FileWriter
try {
FileReader fileReader = new FileReader("/work/test.txt");
FileWriter fileWriter = new FileWriter("/work/test__.txt");
int c = -1;
while ((c = fileReader.read()) > -1) {
fileWriter.write(c);
}
fileWriter.flush();
fileReader.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
5. BufferedReader、BufferedWriter
BufferedReader比上面几种高级的地方在于,上面的几种方式只能能一次读取一个字符,或者一个字符数组。而BufferedReader还能一次读取一行字符串。同时BufferedReader带缓冲,会比前面几种方式快很多。
import java.io.*;
/**
* @author Mr.Guo
* @date 2021/2/9 上午10:04
*/
public class BufferIoTest {
public static void main(String[] args) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader("/work/test.txt"));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/work/buffer.txt")));
if (reader.ready()) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
writer.write(line);
writer.newLine();
}
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
if (null != writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、字节流
字节流以一个字符(即 1 byte,8 bit)作为一个数据单元。
字节流和字符流的使用方式基本一样,唯一的区别就是不能使用char和char[]接收,要使用byte和byte[]用来接收数据。
1. ByteArrayInputStream、ByteArrayOutputStream
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @author Mr.Guo
* @date 2021/2/9 上午11:02
*/
public class ByteIoTest {
public static void main(String[] args) {
ByteArrayInputStream inputStream = new ByteArrayInputStream("大家好,我是码农蝈蝈。".getBytes());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
int c = -1;
while ((c = inputStream.read()) != -1) {
outputStream.write((byte) c);
}
System.out.println(outputStream.toString());
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. FileInputStream、FileOutputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Mr.Guo
* @date 2021/2/9 上午11:17
*/
public class FileByteIoTest {
public static void main(String[] args) {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream("/work/test.zip");
outputStream = new FileOutputStream("/work/copy.zip");
byte[] bytes = new byte[1024];
while (inputStream.read(bytes) != -1) {
outputStream.write(bytes);
}
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != inputStream) {
inputStream.close();
}
if (null != outputStream) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3. PipedInputStream、PipedOutputStream
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;
/**
* @author Mr.Guo
* @date 2021/2/9 下午1:32
*/
public class PipedIoTest {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
public static void main(String[] args) throws IOException {
PipedIoTest piped = new PipedIoTest();
piped.connect();
new Thread() {
@Override
public void run() {
while (true) {
byte[] bytes = new byte[1024];
try {
int len;
while ((len = piped.pis.read(bytes)) != -1) {
System.out.println("len=>" + len);
System.out.println(">>>>>>>>>>>>" + new String(bytes));
}
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
@Override
public void run() {
while (true) {
String tempStr = System.currentTimeMillis() + "" + new Random().nextInt(100000);
try {
piped.pos.write(tempStr.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void connect() throws IOException {
pis.connect(pos);
}
}
各位,piped可以实现线程之间的通信哦~