如何使用FileChannel将一个文件的内容附加到另一个文件的末尾?

文件a.txt看起来像:
ABC

文件d.txt看起来像:

DEF

我正试图拿“DEF”并将其附加到“ABC”,所以a.txt看起来像

ABC
DEF

我尝试过的方法总是完全覆盖第一个条目,所以我总是最终得到:

DEF

以下是我尝试过的两种方法

FileChannel src = new FileInputStream(dFilePath).getChannel(); 
FileChannel dest = new FileOutputStream(aFilePath).getChannel();

src.transferTo(dest.size(),src.size(),dest);

……我试过了

FileChannel src = new FileInputStream(dFilePath).getChannel(); 
FileChannel dest = new FileOutputStream(aFilePath).getChannel();

dest.transferFrom(src,dest.size(),src.size());

API不清楚transferTo和transferFrom param描述:

http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#transferTo(long,long,java.nio.channels.WritableByteChannel)

谢谢你的任何想法.

解决方法

将目标通道的位置移动到结尾:
FileChannel src = new FileInputStream(dFilePath).getChannel(); 
FileChannel dest = new FileOutputStream(aFilePath).getChannel();
dest.position( dest.size() );
src.transferTo(0,dest);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...