Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用NIO写入文件时缺少新行_Java - Fatal编程技术网

Java 使用NIO写入文件时缺少新行

Java 使用NIO写入文件时缺少新行,java,Java,我试图将一个文件的内容复制到一个新文件中,但不知何故,新文件中缺少新行,并将其创建为一行,我猜这与缓冲区位置有关。 遵循我正在使用的代码 List<String> lines; FileChannel destination = null; try { lines = Files.readAllLines(Paths.get(sourceFile.getAbsolutePath()), Charset.defau

我试图将一个文件的内容复制到一个新文件中,但不知何故,新文件中缺少新行,并将其创建为一行,我猜这与缓冲区位置有关。 遵循我正在使用的代码

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();
            }

        }
列表行;
filechanneldestination=null;
尝试
{
lines=Files.readAllLines(path.get(sourceFile.getAbsolutePath()),Charset.defaultCharset());
destination=新文件输出流(destFile).getChannel();
ByteBuffer buf=ByteBuffer.allocate(1024);
用于(字符串行:行)
{
系统输出打印项次(行);
buf.clear();
buf.put(line.getBytes());
buf.flip();
while(buf.haslaining())
{
目的地写入(buf);
}
}
}
最后
{
if(目的地!=null)
{
destination.close();
}
}

写入字节的行:

buf.put(line.getBytes());

…不包括新行字符,您只需写入每行的字节。您需要在每个实例后分别写入新行字符。

写入字节的行:

buf.put(line.getBytes());

…不包括新行字符,您只需写入每行的字节。您需要在每个实例后分别写入新行字符。

您可能更喜欢使用Java 7的文件。复制:

Files.copy(sourceFile.toPath(), destinationFile.toPath(),
        StandardCopyOption.REPLACE_EXISTING);
一个人应该自己写一份文件。
但是,当前版本使用默认平台编码将文件作为文本读取。在UTF-8(一些非法的多字节序列)上出错,在
\u0000
nul字符上,会将行尾转换为默认的平台行尾。

您可能更喜欢使用Java 7的文件。复制:

Files.copy(sourceFile.toPath(), destinationFile.toPath(),
        StandardCopyOption.REPLACE_EXISTING);
一个人应该自己写一份文件。
但是,当前版本使用默认平台编码将文件作为文本读取。这在UTF-8(一些非法的多字节序列)上出错,在
\u0000
nul字符上,将行结尾转换为默认的平台结尾。

Do
buff.put(System.getProperty(“line.separator”).toString()
before
buf.put(line.getBytes())

Do
buff.put(System.getProperty(“line.separator”).toString()
before
buf.put(line.getBytes())

这将包括新行:

   ByteBuffer bf = null;
   final String newLine = System.getProperty("line.separator");
   bf = ByteBuffer.wrap((yourString+newLine).getBytes(Charset.forName("UTF-8" )));

这将包括新的生产线:

   ByteBuffer bf = null;
   final String newLine = System.getProperty("line.separator");
   bf = ByteBuffer.wrap((yourString+newLine).getBytes(Charset.forName("UTF-8" )));

您可以直接使用
System.lineSeparator()
代替
System.getProperty(“line.separator”)


您可以直接使用
System.lineSeparator()
代替
System.getProperty(“line.separator”)


你从不写新行字符!(您的误解可能是由于
readAllLines
的行为:它删除了行分隔符)问题是它应该自动添加,还是我需要在阅读每一行后将其添加到循环中?您从未编写新行字符!(您的误解可能是由于
readAllLines
的行为:它删除了行分隔符)问题是它应该自动添加,还是我需要在阅读每一行后将其添加到循环中?我在循环中的行中添加了新行,它工作正常,我的问题是,如果我有一个包含3个不同行的列表,为什么会发生这种情况“行”-说“第1行”、“第2行”和“第3行”,那么这些行中没有任何新行字符。因此,如果您要将它们写出来,您需要手动将它们放回(否则您会看到您正在观察的行为)我在循环内的行中添加了新行,它工作正常,我的问题是为什么会发生这种情况如果我有一个包含3个不同“行”的列表,比如“行1”、“行2”和“行3”,那么这些行中没有任何新行字符。因此,如果你要写出它们,你需要手动将它们放回(否则,您将看到您正在观察的行为。)