.NET File.WriteAllLines留下空行,在文件的结尾 [英] .NET File.WriteAllLines leaves empty line at the end of file

查看:1713
本文介绍了.NET File.WriteAllLines留下空行,在文件的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用节省的System.IO.File.WriteAllLines String []数组的内容,在文件的结尾总是留一个空行。例如:

When I'm saving content of the String[] array with System.IO.File.WriteAllLines, at the end of a file is always left a blank line. For example:

System.IO.File.WriteAllLines(Application.dataPath + "/test.txt",["a", "b", "c"]);

生成文件(不带下划线):

Produces file (without underscore):

a
b
c
_

已经有这样的题目:<一href="http://stackoverflow.com/questions/8893361/empty-line-in-net-file-writealllines-is-a-bug">Empty在净File.WriteAllLines线,是一个错误?,但作者日期说,我觉得有什么毛病我的数据,这是我的问题,但不是WritAllLines,它被关闭,太本地化( ?!?)。

There was already such topic: Empty line in .Net File.WriteAllLines, is a bug? , but autor said that "I think there are something wrong with my data,that's my problem but not the WritAllLines" and it was closed as "too localized" (?!?).

这是一个错误?我怎么能轻松地摆脱它(现在我在读文件时,只需忽略它再次)?

It's a bug? How can I easily get rid of it (for now I'm just ignoring it when reading file again)?

推荐答案

WriteAllLines 方法会写在你的阵列后跟一个换行符每一行。这意味着你将永远得到你的文件这个空行。

The WriteAllLines method will write out each line in your array followed by a line break. This means that you will always get this "empty line" in your file.

在您链接后提出的一点是,当运行 ReadAllLines 即认为线是由一个换行符终止字符。所以,当你在你刚刚写入的文件中使用的读取方法,你应该得到完全相同的行了。

The point made in the post you linked is that when running ReadAllLines that considers a line to be characters terminated by a line break. So when you use the read method on the file you've just written you should get the exact same lines back.

如果您正在阅读的文件,以不同的方式,那么你将不得不面对换行符自己。

If you are reading the file in a different way then you will have to deal with linebreaks yourself.

从本质上讲,你所看到的是预期的行为。

Essentially what you are seeing is Expected Behaviour.

这篇关于.NET File.WriteAllLines留下空行,在文件的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆