如何使用FileChannel将一个文件的内容附加到另一个文件的末尾?
发布时间:2020-05-25 17:23:10 所属栏目:Java 来源:互联网
导读:文件a.txt看起来像: ABC 文件d.txt看起来像: DEF 我正试图拿“DEF”并将其附加到“ABC”,所以a.txt看起来像 ABCDEF 我尝试过的方法总是完全覆盖第一个条目,所以我总是最终得到: DEF 以下是我尝试过的两种方法: FileChannel src = new FileInputStream(dFi
文件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); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |