利用FileChannel完成文件的读、写、复制
2017-08-11 17:46
357 查看
内容:通过NIO中的FileChannel完成文件的读、写、复制。
[java] view
plain copy
public class NioFileCopy {
private RandomAccessFile aFile = null;
private FileChannel inChannel = null;
private final ByteBuffer buf = ByteBuffer.allocate(1024);
public void doWrite() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
String newData = "New String to wirte to file... " + System.currentTimeMillis();
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while (buf.hasRemaining())
inChannel.write(buf);
inChannel.close();
System.out.println("Write Over");
}
public void doRead() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip();
while (buf.hasRemaining())
System.out.print((char) buf.get());
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
}
public void doCopy() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
RandomAccessFile bFile = new RandomAccessFile("C:/22.log", "rw");
FileChannel outChannel = bFile.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
System.out.println("Copy over");
}
public static void main(String[] args) throws IOException {
NioFileCopy tool = new NioFileCopy();
//tool.doWrite();
//tool.doRead();
tool.doCopy();
}
}
[java] view
plain copy
public class NioFileCopy {
private RandomAccessFile aFile = null;
private FileChannel inChannel = null;
private final ByteBuffer buf = ByteBuffer.allocate(1024);
public void doWrite() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
String newData = "New String to wirte to file... " + System.currentTimeMillis();
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while (buf.hasRemaining())
inChannel.write(buf);
inChannel.close();
System.out.println("Write Over");
}
public void doRead() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip();
while (buf.hasRemaining())
System.out.print((char) buf.get());
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
}
public void doCopy() throws IOException {
aFile = new RandomAccessFile("C:/goods.txt", "rw");
inChannel = aFile.getChannel();
RandomAccessFile bFile = new RandomAccessFile("C:/22.log", "rw");
FileChannel outChannel = bFile.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
System.out.println("Copy over");
}
public static void main(String[] args) throws IOException {
NioFileCopy tool = new NioFileCopy();
//tool.doWrite();
//tool.doRead();
tool.doCopy();
}
}
相关文章推荐
- 利用FileChannel完成文件的读、写、复制
- 利用FileChannel复制文件 Copy one File to Another【三种方法】
- 利用FileChannel复制文件
- 利用Asp.net IO.File类完成文件新建复制删除操作
- 利用Asp.net IO.File类完成文件新建复制删除操作(代码调试通过)(转)
- 利用FileReader和FileWriter完成一个文件拷贝功能
- 利用spring中的FileCopyUtils.copy()复制文件
- 利用spring中的FileCopyUtils.copy()复制文件
- 利用filechannel拷贝文件内容
- FileSystemWatcher判断文件复制完成
- 190_IO流_FileReader_Writer完成文件复制
- Java中用文件通道(FileChannel)、BufferedInputStream/BufferedOutputStream复制文件对比
- 利用spring中的FileCopyUtils.copy()复制文件
- IOの利用字节流和字符流完成文件的复制demo
- Java 复制大文件方式FileChannel 用法
- FileChannel-Java 复制文件的高效方法
- jar文件的复制和解压(JarInputStream,JarOutputStream和JarFile)
- 利用InputStream和OutputStream流完成文件的断点续传,(原理)
- 利用RamdonAccessFile来实现文件的追加
- 编写方法,完成指定文件的复制和剪切;使用BufferedReader和BufferedWriter完成。