Java基础之读文件——使用通道复制文件(FileBackup)
控制台程序,除了使用Files类中使用copy()方法将文件复制外,还可以使用FileChannel对象复制文件,连接到输入文件的FileChannel对象能直接将数据传输到连接到输出文件的FileChannel对象中而不涉及显式的缓冲区。
本例用来对命令行参数设定的文件进行复制。文件被复制到一个类似在原始目录下创建的备份文件中。新文件的名称通过在原始文件名称的后面附加多次“_backup”以获得唯一文件名。
import static java.nio.file.StandardOpenOption.*;
import java.nio.file.*;
import java.nio.channels.*;
import java.io.IOException;
import java.util.EnumSet; public class FileBackup {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("No file to copy. Application usage is:\n" + "java -classpath . FileCopy \"filepath\"" );
System.exit(1);
}
Path fromFile = Paths.get(args[0]);
fromFile.toAbsolutePath(); if(Files.notExists(fromFile)) {
System.out.printf("File to copy, %s, does not exist.", fromFile);
System.exit(1);
} Path toFile = createBackupFilePath(fromFile);
try (FileChannel inCh = (FileChannel)(Files.newByteChannel(fromFile));
WritableByteChannel outCh = Files.newByteChannel( toFile, EnumSet.of(WRITE,CREATE_NEW))){
int bytesWritten = 0;
long byteCount = inCh.size();
while(bytesWritten < byteCount) {
bytesWritten += inCh.transferTo(bytesWritten, byteCount-bytesWritten, outCh);
} System.out.printf("File copy complete. %d bytes copied to %s%n", byteCount, toFile);
} catch(IOException e) {
e.printStackTrace();
}
} // Method to create a unique backup Path object under MS Windows
public static Path createBackupFilePath(Path file) {
Path parent = file.getParent();
String name = file.getFileName().toString(); // Get the file name
int period = name.indexOf('.'); // Find the extension separator
if(period == -1) { // If there isn't one
period = name.length(); // set it to the end of the string
}
String nameAdd = "_backup"; // String to be appended // Create a Path object that is a unique
Path backup = parent.resolve(
name.substring(0,period) + nameAdd + name.substring(period));
while(Files.exists(backup)) { // If the path already exists...
name = backup.getFileName().toString(); // Get the current file name
backup = parent.resolve(name.substring(0,period) + // add _backup
nameAdd + name.substring(period));
period += nameAdd.length(); // Increment separator index
}
return backup;
}
}
Java基础之读文件——使用通道复制文件(FileBackup)的更多相关文章
- Java使用文件通道复制文件
两种文件通道复制文件方式的性能比较 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...
- Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...
- Java基础-IO流对象之随机访问文件(RandomAccessFile)
Java基础-IO流对象之随机访问文件(RandomAccessFile) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.RandomAccessFile简介 此类的实例支持对 ...
- Java基础之读文件——使用通道读取混合数据2(ReadPrimesMixedData2)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法二:设置一个任意容量的.大小合适的字节缓冲区并且使用来自文件的字节进行填充.然后整理出缓冲区 ...
- Java基础之读文件——使用通道读取混合数据1(ReadPrimesMixedData)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile2)写入的Primes.txt. 方法一:可以在第一个读操作中读取字符串的长度,然后再将字符串和二进制素数值读入到文本中.这种方式 ...
- Java基础之读文件——使用通道读二进制数据(ReadPrimes)
控制台程序,本例读取Java基础之写文件部分(PrimesToFile)写入的primes.bin. import java.nio.file.*; import java.nio.*; import ...
- Java基础之读文件——从文件中读取文本(ReadAString)
控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt import java.nio.file.*; import java ...
- Java基础之读文件——使用缓冲读取器读取文件(ReaderInputFromFile)
控制台程序,本例读取Java基础之写文件部分(WriterOutputToFile)写入的Saying.txt. import java.io.*; import java.nio.file.*; i ...
- Java基础——NIO(一)通道与缓冲区
一.概述 1.什么是NIO NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多. 在Java ...
随机推荐
- vFloppy1.5-虚拟启动软盘
vFloppy1.5为纯绿色免费软件,安装后不需要引导盘即可进入DOS,支持图形化访问NFTS系统格式,还可添加由光盘启动菜单选项,对于没有光驱,软驱的朋友非常实用. 到BIOS设置由光盘启动,或者打 ...
- MySQL 性能优化的最佳20多条经验分享[转]
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情. 当我们去设计数据库表结构, ...
- Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件
最近Linux服务器磁盘使用量经常到100%,直到影响到正常服务出现故障才会去注意,做不到防患于未然,今天在网上搜集了资料,加上自己修改,写了一个shell脚本用于实时监控磁盘使用量并在超过阀值后自动 ...
- the computer spends over 96% of its time waiting for I/O devices to finish transferring data
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- processor, memory, I/O
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 3.3 INTERCONNECTION S ...
- changing a pointer rather than erasing memory cells
Computer Science An Overview _J. Glenn Brookshear _11th Edition In a data structure based on a point ...
- P1017 进制转换
模拟水题,直接上代码 #include <bits/stdc++.h> using namespace std; const int maxn = 100000; int main() { ...
- Python - 求斐波那契数列前N项之和
n = int(input("Input N: ")) a = 0 b = 1 sum = 0 for i in range(n): sum += a a, b = b, a + ...
- freebsd上安装nginx+php记录
参考文章 https://wiki.freebsdchina.org/faq/ports http://www.vpsee.com/2014/04/install-nginx-php-apc-mysq ...
- 切记CMYK图片格式在IE中将无法显示
目前为止微软的Internet Explorer 浏览器IE6,IE7,IE8都不支持CMYK颜色模式图像 ,除IE外其他浏览器均能支持!所以大家要注意了 要选择RGB颜色模式,就可以了.