我正在尝试将一个文件的内容复制到新文件中,并且不知何故新文件中缺少新行并将其创建为一行,我猜它与缓冲区位置有关。按照我正在使用的代码..
List<String> lines;
FileChannel destination = null;
try
{
lines = Files.readAllLines(Paths.get(sourceFile.getAbsolutePath()), Charset.defaultCharset());
destination = new FileOutputStream(destFile).getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
for (String line : lines)
{
System.out.println(line);
buf.clear();
buf.put(line.getBytes());
buf.flip();
while (buf.hasRemaining())
{
destination.write(buf);
}
}
}
finally
{
if (destination != null)
{
destination.close();
}
}