文件a.txt看起来像:
ABC
Run Code Online (Sandbox Code Playgroud)
文件d.txt看起来像:
DEF
Run Code Online (Sandbox Code Playgroud)
我正试图拿"DEF"并将其附加到"ABC",所以a.txt看起来像
ABC
DEF
Run Code Online (Sandbox Code Playgroud)
我尝试过的方法总是完全覆盖第一个条目,所以我总是最终得到:
DEF
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的两种方法:
FileChannel src = new FileInputStream(dFilePath).getChannel();
FileChannel dest = new FileOutputStream(aFilePath).getChannel();
src.transferTo(dest.size(), src.size(), dest);
Run Code Online (Sandbox Code Playgroud)
......我试过了
FileChannel src = new FileInputStream(dFilePath).getChannel();
FileChannel dest = new FileOutputStream(aFilePath).getChannel();
dest.transferFrom(src, dest.size(), src.size());
Run Code Online (Sandbox Code Playgroud)
API不清楚transferTo和transferFrom param描述:
谢谢你的任何想法.
Alk*_*ris 13
这是旧的,但由于您打开文件输出流的模式而发生覆盖.对于任何需要此功能的人,请尝试
FileChannel src = new FileInputStream(dFilePath).getChannel();
FileChannel dest = new FileOutputStream(aFilePath, true).getChannel(); //<---second argument for FileOutputStream
dest.position( dest.size() );
src.transferTo(0, src.size(), dest);
Run Code Online (Sandbox Code Playgroud)
将目标通道的位置移到末尾:
FileChannel src = new FileInputStream(dFilePath).getChannel();
FileChannel dest = new FileOutputStream(aFilePath).getChannel();
dest.position( dest.size() );
src.transferTo(0, src.size(), dest);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5452 次 |
| 最近记录: |