一、简介
1.1 IO(BIO)---阻塞式IO
起源于JDK1.0
- java.io 包几乎包含了所有操作输入、输出需要的类。
- 所有这些流类代表了输入源和输出目标。
- java.io 包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。
一个流可以理解为一个数据的序列。
- 输入流表示从一个源读取数据,输出流表示向一个目标写数据。
- Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。
1.2 NIO---非阻塞式IO
起源于JDK1.4
- Java NIO可以让你非阻塞的使用IO,例如:当线程从通道读取数据到缓冲区时,线程还是可以进行其他事情。当数据被写入到缓冲区时,线程可以继续处理它。从缓冲区写入通道也类似。
1.3 BIO和NIO对比
- 标准的IO基于字节流和字符流进行操作的,而NIO是基于
通道(Channel
)和缓冲区(Buffer)
进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。
二、使用
2.1 BIO使用
2.1.1 读取文件内容到控制台
/**
* IO读取文件到控制台
*/
public static void readFile() {
File file = new File("D:\\desktop\\io.txt");
try {
FileReader fr = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fr);
String str = bufferedReader.readLine();
while (str != null) {
System.out.println(str);
str = bufferedReader.readLine();//将reader置为空
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.1.2 写入文本内容到文件中
/**
* IO写入(生成)文件
*/
public static void writeFile() {
File file = new File("D:\\desktop\\io2.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
String str = "Hello Write File";
outputStream.write(str.getBytes());
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.1.3 复制文件
/**
* IO复制文件
*/
public static void copyFile() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream("D:\\desktop\\Linux软件安装包.zip"); //文件大小387M
outputStream = new FileOutputStream("D:\\desktop\\Linux软件安装包_cp.zip");
byte[] buf = new byte[1024];
int len = -1;
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.2 NIO使用
2.2.1 NIO方式读取文件内容
/**
* NIO读取文件
*/
public static void read() {
RandomAccessFile access = null;
try {
access = new RandomAccessFile(new File("D:\\desktop\\io.txt"), "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FileChannel channel = access.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
CharBuffer charBuffer = CharBuffer.allocate(1024);
Charset charset = Charset.forName("utf-8");
CharsetDecoder decoder = charset.newDecoder();
int length = 0;
try {
length = channel.read(byteBuffer);
while (length != -1) {
byteBuffer.flip();
decoder.decode(byteBuffer, charBuffer, true);
charBuffer.flip();
System.out.println(charBuffer.toString());
if (byteBuffer != null) byteBuffer.clear();
if (charBuffer != null) charBuffer.clear();
length = channel.read(byteBuffer); // 再次读取文本内容
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (channel != null) channel.close();
if (access != null) access.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2.2 NIO方式写入文件
/**
* NIO写文件
*
* @param context
* @throws IOException
*/
public static void write(String context) {
FileOutputStream outputStream = null;
FileChannel channel = null;
try {
outputStream = new FileOutputStream(new File("D:\\desktop\\nio.txt"), true);//允许文件内容追加而不是覆盖
channel = outputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put(context.getBytes("utf-8"));
byteBuffer.flip();//读取模式转换为写入模式
channel.write(byteBuffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (channel != null) channel.close();
if (outputStream != null) outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2.3 NIO方式复制文件
/**
* NIO复制文件
*
* @param source
* @param target
*/
public static void nioCopy(String source, String target) {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
FileInputStream inputStream = null;
FileChannel inChannel = null;
FileOutputStream outputStream = null;
FileChannel outChannel = null;
try {
inputStream = new FileInputStream(source);
inChannel = inputStream.getChannel();
outputStream = new FileOutputStream(target);
outChannel = outputStream.getChannel();
int length = 0;
length = inChannel.read(byteBuffer);
while (length != -1) {
byteBuffer.flip();//读取模式转换写入模式
outChannel.write(byteBuffer);
byteBuffer.clear(); //清空缓存,等待下次写入
length = inChannel.read(byteBuffer); // 再次读取文本内容
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
outChannel.close();
inputStream.close();
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}