前置文章:
Netty基礎(chǔ)-NIO(一)棚唆,該文主要引入NIO三大組件,介紹了Buffer結(jié)構(gòu)冗栗,及Buffer具體使用演顾。
零、本文綱要
一隅居、Channel
- FileChannel
- FileChannel傳輸
二钠至、補(bǔ)充:Path & Paths & Files 類(lèi)
- Path & Paths
- Files
- Files 類(lèi)的 walkFileTree方法 & walk方法
一、Channel
1. FileChannel
注意:FileChannel 只能工作在阻塞模式下
① 獲取方式
FileInputStream → 只讀胎源;
FileOutputStream → 只寫(xiě)棉钧;
RandomAccessFile → 讀寫(xiě);
FileChannel readChannel = new FileInputStream("src/main/resources/data.txt").getChannel();
FileChannel writeChannel = new FileOutputStream("src/main/resources/data_back.txt").getChannel();
final RandomAccessFile rwChannel = new RandomAccessFile("src/main/resources/data.txt", "rw");
② read方法
讀數(shù)據(jù)到buffer涕蚤,返回值表示讀到了多少字節(jié)宪卿,0 / -1 表示到達(dá)了文件的末尾。
ByteBuffer buffer = ByteBuffer.allocate(16);
FileChannel readChannel = new FileInputStream("src/main/resources/data.txt").getChannel();
// TODO 只讀Channel中讀取數(shù)據(jù)万栅,填充至Buffer
int read = readChannel.read(buffer);
log.debug("當(dāng)前讀取了:{}字節(jié)內(nèi)容愧捕。", read);
debugAll(buffer);
readChannel.close();
注意:此處官方一般用 -1 來(lái)做讀完數(shù)據(jù)的標(biāo)志。
③ channel#write方法 & buffer#hasRemaining方法
FileChannel writeChannel = new FileOutputStream("src/main/resources/data_back.txt").getChannel();
while (buffer.hasRemaining()) {
writeChannel.write(buffer);
}
writeChannel.close();
④ close方法
因?yàn)樯婕暗劫Y源流處理申钩,使用完關(guān)閉資源是良好的習(xí)慣。
⑤ position方法
該方法用于獲取position指針的當(dāng)前位置
log.debug("buffer position is: {}", buffer.position());
log.debug("channel position is: {}", writeChannel.position());
buffer position is: 11
channel position is: 11
⑥ size方法
該方法獲取能文件的大小
⑦ force方法
操作系統(tǒng)出于性能的考慮瘪阁,會(huì)將數(shù)據(jù)緩存撒遣,不是立刻寫(xiě)入磁盤(pán)。
可以調(diào)用 force(true) 方法將文件內(nèi)容和元數(shù)據(jù)(文件的權(quán)限等信息)立刻寫(xiě)入磁盤(pán)管跺。
2. FileChannel傳輸
① 傳統(tǒng)的方式
readChannel 從文件讀取數(shù)據(jù)至 buffer 义黎,而后從 buffer 取數(shù)據(jù)通過(guò) writeChannel 寫(xiě)數(shù)據(jù)至文件
ByteBuffer buffer = ByteBuffer.allocate(16);
FileChannel readChannel = new FileInputStream("src/main/resources/data.txt").getChannel();
int read = readChannel.read(buffer);
log.debug("當(dāng)前讀取了:{}字節(jié)內(nèi)容。", read);
debugAll(buffer);
readChannel.close();
buffer.flip(); //注意:要手動(dòng)將position重置為0
FileChannel writeChannel = new FileOutputStream("src/main/resources/data_back.txt").getChannel();
while (buffer.hasRemaining()) {
writeChannel.force(true);
writeChannel.write(buffer);
log.debug("buffer position is: {}", buffer.position());
log.debug("channel position is: {}", writeChannel.position());
}
writeChannel.close();
② transferTo方法
final String IN_FILE = "src/main/resources/data.txt";
final String OUT_FILE = "src/main/resources/data_back.txt";
try (
FileChannel readChannel = new FileInputStream(IN_FILE).getChannel();
FileChannel writeChannel = new FileOutputStream(OUT_FILE).getChannel();
) {
readChannel.transferTo(0, readChannel.size(), writeChannel);
} catch (IOException e) {
log.debug(e.getMessage());
}
二豁跑、補(bǔ)充:Path & Paths & Files 類(lèi)
1. Path & Paths
Path:用來(lái)表示文件路徑
Paths:用于獲取Path的工具類(lèi)
String URI = "src/main/resources/data.txt";
String URL = "D:\\JavaStudy\\Level1\\netty-nio\\src\\main\\resources\\data.txt";
Path sourceOne = Paths.get(URI);
Path sourceTwo = Paths.get(URL);
2. Files
① exists方法
用于判斷文件是否存在廉涕。
log.debug("Is file exists? {}", Files.exists(sourceOne));
② createDirectory方法 & createDirectories方法
用于創(chuàng)建目錄/多級(jí)目錄的方法。
createDirectory方法
a艇拍、如果目錄已存在狐蜕,會(huì)拋異常 FileAlreadyExistsException;
b卸夕、不能一次創(chuàng)建多級(jí)目錄层释,否則會(huì)拋異常 NoSuchFileException。
c快集、createDirectories方法可以創(chuàng)建多級(jí)目錄贡羔。
③ copy方法
用于拷貝文件的方法廉白。
Path source = Paths.get("stone/source.txt");
Path backup = Paths.get("stone/backup.txt");
Files.copy(source, backup);
注意:如果文件已存在,會(huì)拋異常 FileAlreadyExistsException乖寒。
StandardCopyOption可以用于控制copy文件的形式猴蹂,REPLACE_EXISTING用于覆蓋已存在的backup文件,如下:
Files.copy(source, backup, StandardCopyOption.REPLACE_EXISTING);
④ move方法
用于移動(dòng)文件楣嘁,StandardCopyOption.ATOMIC_MOVE 保證文件移動(dòng)的原子性磅轻,如下:
Files.move(source, backup, StandardCopyOption.ATOMIC_MOVE);
⑤ delete方法
用于刪除目錄的方法。
Path target = Paths.get("stone/directory");
Files.delete(target);
注意:如果目錄還有內(nèi)容马澈,會(huì)拋異常 DirectoryNotEmptyException瓢省。
3. Files 類(lèi)的 walkFileTree方法 & walk方法
① 遍歷目錄文件
此處我們重寫(xiě) SimpleFileVisitor 內(nèi)部類(lèi)的 preVisitDirectory、visitFile 方法
final String JAVA_PATH = "C:\\Program Files\\Java\\jdk1.8.0_311";
Path path = Paths.get(JAVA_PATH);
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
log.debug(dir.getFileName().toString());
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
log.debug(file.getFileName().toString());
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
});
log.debug("directory count is: {}.", dirCount);
log.debug("file count is: {}.", fileCount);
② 統(tǒng)計(jì)指定文件類(lèi)型的數(shù)目
比如統(tǒng)計(jì) .jar包 的數(shù)量
final String JAVA_PATH = "C:\\Program Files\\Java\\jdk1.8.0_311";
Path path = Paths.get(JAVA_PATH);
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String fileName = file.getFileName().toString();
if (fileName.endsWith(".jar")) {
log.debug("{}", fileName);
fileCount.incrementAndGet();
}
return super.visitFile(file, attrs);
}
});
log.debug("file count is: {}.", fileCount);
③ 刪除多級(jí)目錄
注意:謹(jǐn)慎操作
final String DELETE_PATH = "D:\\use-to-delete";
Path path = Paths.get(DELETE_PATH);
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
fileCount.incrementAndGet();
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
dirCount.incrementAndGet();
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
log.debug("The deleted file count is: {}.", fileCount);
log.debug("The deleted directories count is: {}.", dirCount);
④ 拷貝多級(jí)目錄
final String SOURCE = "D:\\logs";
final String BACKUP = "D:\\logs-backup";
Files.walk(Paths.get(SOURCE)).forEach( path -> {
try {
String backupName = path.toString().replace(SOURCE, BACKUP);
if (Files.isDirectory(path)) {
Files.createDirectory(Paths.get(backupName));
}
else if (Files.isRegularFile(path)) {
Files.copy(path, Paths.get(backupName));
}
} catch (IOException e) {
log.debug(e.getMessage());
}
});
⑤ 補(bǔ)充:SimpleFileVisitor類(lèi)
屬性Spring框架的話這部分是不是非常熟悉類(lèi)似痊班,此處不展開(kāi)勤婚。
a、preVisitDirectory:訪問(wèn)目錄前涤伐;
b馒胆、visitFile:訪問(wèn)文件;
c凝果、visitFileFailed:訪問(wèn)不可訪問(wèn)的文件(無(wú)權(quán)限)祝迂;
d、postVisitDirectory:訪問(wèn)目錄后器净。
public class SimpleFileVisitor<T> implements FileVisitor<T> {
protected SimpleFileVisitor() {
}
@Override
public FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
throws IOException
{
Objects.requireNonNull(dir);
Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(T file, BasicFileAttributes attrs)
throws IOException
{
Objects.requireNonNull(file);
Objects.requireNonNull(attrs);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException
{
Objects.requireNonNull(file);
throw exc;
}
@Override
public FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException
{
Objects.requireNonNull(dir);
if (exc != null)
throw exc;
return FileVisitResult.CONTINUE;
}
}
三型雳、結(jié)尾
以上即為Netty基礎(chǔ)-NIO(二)的全部?jī)?nèi)容,感謝閱讀山害。
注意:該文集系列鋪墊的內(nèi)容會(huì)稍微有些多纠俭。