小編這次要給大家分享的是Java如何使用BIO和NIO進(jìn)行文件操作對比,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括霍林郭勒網(wǎng)站建設(shè)、霍林郭勒網(wǎng)站制作、霍林郭勒網(wǎng)頁制作以及霍林郭勒網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,霍林郭勒網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到霍林郭勒省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
什么是Java NIO
同步非阻塞io模式,拿燒開水來說,NIO的做法是叫一個線程不斷的輪詢每個水壺的狀態(tài),看看是否有水壺的狀態(tài)發(fā)生了改變,從而進(jìn)行下一步的操作。
Java NIO有三大組成部分:Buffer,Channel,Selector,通過事件驅(qū)動模式實現(xiàn)了什么時候有數(shù)據(jù)可讀的問題。
什么是Java BIO?
同步阻塞IO模式,數(shù)據(jù)的讀取寫入必須阻塞在一個線程內(nèi)等待其完成。這里使用那個經(jīng)典的燒開水例子,這里假設(shè)一個燒開水的場景,有一排水壺在燒開水,BIO的工作模式就是, 叫一個線程停留在一個水壺那,直到這個水壺?zé)_,才去處理下一個水壺。但是實際上線程在等待水壺?zé)_的時間段什么都沒有做。不知道io操作中什么時候有數(shù)據(jù)可讀,所以一直是阻塞的模式。
1、讀文件
package com.zhi.test;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 文件讀取,緩沖區(qū)大小(BF_SIZE)對NIO的性能影響特別大,對BIO無影響<br>
* 10M的文件,BIO耗時87毫秒,NIO耗時68毫秒,F(xiàn)iles.read耗時62毫秒
*
* @author 張遠(yuǎn)志
* @since 2020年5月9日19:20:49
*
*/
public class FileRead {
/**
* 緩沖區(qū)大小
*/
private static final int BF_SIZE = 1024;
/**
* 使用BIO讀取文件
*
* @param fileName 待讀文件名
* @return
* @throws IOException
*/
public static String bioRead(String fileName) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileReader reader = new FileReader(fileName);
StringBuffer buf = new StringBuffer();
char[] cbuf = new char[BF_SIZE];
while (reader.read(cbuf) != -1) {
buf.append(cbuf);
}
reader.close();
return buf.toString();
} finally {
System.out.println("使用BIO讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用NIO讀取文件
*
* @param fileName 待讀文件名
* @return
* @throws IOException
*/
public static String nioRead1(String fileName) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileInputStream input = new FileInputStream(fileName);
FileChannel channel = input.getChannel();
CharsetDecoder decoder = Charset.defaultCharset().newDecoder();
StringBuffer buf = new StringBuffer();
CharBuffer cBuf = CharBuffer.allocate(BF_SIZE);
ByteBuffer bBuf = ByteBuffer.allocate(BF_SIZE);
while (channel.read(bBuf) != -1) {
bBuf.flip();
decoder.decode(bBuf, cBuf, false); // 解碼,byte轉(zhuǎn)char,最后一個參數(shù)非常關(guān)鍵
bBuf.clear();
buf.append(cBuf.array(), 0, cBuf.position());
cBuf.compact(); // 壓縮
}
input.close();
return buf.toString();
} finally {
System.out.println("使用NIO讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用Files.read讀取文件
*
* @param fileName 待讀文件名
* @return
* @throws IOException
*/
public static String nioRead2(String fileName) throws IOException {
long startTime = System.currentTimeMillis();
try {
byte[] byt = Files.readAllBytes(Paths.get(fileName));
return new String(byt);
} finally {
System.out.println("使用Files.read讀取文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
public static void main(String[] args) throws IOException {
String fileName = "E:/source.txt";
FileRead.bioRead(fileName);
FileRead.nioRead1(fileName);
FileRead.nioRead2(fileName);
}
}2、寫文件
package com.zhi.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
/**
* 文件寫<br>
* 10M的數(shù)據(jù),BIO耗時45毫秒,NIO耗時42毫秒,F(xiàn)iles.write耗時24毫秒
*
* @author 張遠(yuǎn)志
* @since 2020年5月9日21:04:40
*
*/
public class FileWrite {
/**
* 使用BIO進(jìn)行文件寫
*
* @param fileName 文件名稱
* @param content 待寫內(nèi)存
* @throws IOException
*/
public static void bioWrite(String fileName, String content) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileWriter writer = new FileWriter(fileName);
writer.write(content);
writer.close();
} finally {
System.out.println("使用BIO寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用NIO進(jìn)行文件寫
*
* @param fileName 文件名稱
* @param content 待寫內(nèi)存
* @throws IOException
*/
public static void nioWrite1(String fileName, String content) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileOutputStream out = new FileOutputStream(fileName);
FileChannel channel = out.getChannel();
ByteBuffer buf = ByteBuffer.wrap(content.getBytes());
channel.write(buf);
out.close();
} finally {
System.out.println("使用NIO寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用Files.write進(jìn)行文件寫
*
* @param fileName 文件名稱
* @param content 待寫內(nèi)存
* @throws IOException
*/
public static void nioWrite2(String fileName, String content) throws IOException {
long startTime = System.currentTimeMillis();
try {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
Files.write(file.toPath(), content.getBytes(), StandardOpenOption.WRITE);
} finally {
System.out.println("使用Files.write寫文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
public static void main(String[] args) throws IOException {
String content = FileRead.nioRead2("E:/source.txt");
String target1 = "E:/target1.txt", target2 = "E:/target2.txt", target3 = "E:/target3.txt";
FileWrite.bioWrite(target1, content);
FileWrite.nioWrite1(target2, content);
FileWrite.nioWrite2(target3, content);
}
}3、復(fù)制文件
package com.zhi.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 文件復(fù)制<br>
* 10M的文件,bio耗時56毫秒,nio耗時12毫秒,F(xiàn)iles.copy耗時10毫秒
*
* @author 張遠(yuǎn)志
* @since 2020年5月9日17:18:01
*
*/
public class FileCopy {
/**
* 使用BIO復(fù)制一個文件
*
* @param target 源文件
* @param source 目標(biāo)文件
*
* @throws IOException
*/
public static void bioCopy(String source, String target) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileInputStream fin = new FileInputStream(source);
FileOutputStream fout = new FileOutputStream(target);
byte[] byt = new byte[1024];
while (fin.read(byt) > -1) {
fout.write(byt);
}
fin.close();
fout.close();
} finally {
System.out.println("使用BIO復(fù)制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用NIO復(fù)制一個文件
*
* @param target 源文件
* @param source 目標(biāo)文件
*
* @throws IOException
*/
public static void nioCopy1(String source, String target) throws IOException {
long startTime = System.currentTimeMillis();
try {
FileInputStream fin = new FileInputStream(source);
FileChannel inChannel = fin.getChannel();
FileOutputStream fout = new FileOutputStream(target);
FileChannel outChannel = fout.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
fin.close();
fout.close();
} finally {
System.out.println("使用NIO復(fù)制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
/**
* 使用Files.copy復(fù)制一個文件
*
* @param target 源文件
* @param source 目標(biāo)文件
*
* @throws IOException
*/
public static void nioCopy2(String source, String target) throws IOException {
long startTime = System.currentTimeMillis();
try {
File file = new File(target);
if (file.exists()) {
file.delete();
}
Files.copy(Paths.get(source), file.toPath());
} finally {
System.out.println("使用Files.copy復(fù)制文件耗時:" + (System.currentTimeMillis() - startTime) + "毫秒");
}
}
public static void main(String[] args) throws IOException {
String source = "E:/source.txt";
String target1 = "E:/target1.txt", target2 = "E:/target2.txt", target3 = "E:/target3.txt";
FileCopy.bioCopy(source, target1);
FileCopy.nioCopy1(source, target2);
FileCopy.nioCopy2(source, target3);
}
}看完這篇關(guān)于Java如何使用BIO和NIO進(jìn)行文件操作對比的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
標(biāo)題名稱:Java如何使用BIO和NIO進(jìn)行文件操作對比
鏈接地址:http://www.chinadenli.net/article42/geipec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、Google、營銷型網(wǎng)站建設(shè)、小程序開發(fā)、服務(wù)器托管、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)