Reputation: 163
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
_
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)?
Upvotes: 16
Views: 14073
Reputation: 326
You can also save a file with WriteAllText and join array of lines manually like:
File.WriteAllText(file, String.Join("\r\n",correctedLines));
Upvotes: 10
Reputation: 394
There's a simpler workaround:
// 1. Convert the items on the array to single string with the separator "\n" between the items
string AllItemsInOneString= string.Join("\n", StringArrayToSave);
// 2. Save with WriteAllText instead
File.WriteAllText(FilePath, AllItemsInOneString);
Upvotes: 4
Reputation: 50336
As others have pointed out, that's just how it works. Here is a method that does it without the extra newline:
public static class FileExt
{
public static void WriteAllLinesBetter(string path, params string[] lines)
{
if (path == null)
throw new ArgumentNullException("path");
if (lines == null)
throw new ArgumentNullException("lines");
using (var stream = File.OpenWrite(path))
using (StreamWriter writer = new StreamWriter(stream))
{
if (lines.Length > 0)
{
for (int i = 0; i < lines.Length - 1; i++)
{
writer.WriteLine(lines[i]);
}
writer.Write(lines[lines.Length - 1]);
}
}
}
}
Usage:
FileExt.WriteAllLinesBetter("test.txt", "a", "b", "c");
Writes:
aenter benter c
Upvotes: 17
Reputation: 743
@Virtlink solution is almost perfect. In fact there is a scenario where you will get garbage at the end of the file - when the file exist and its content is bigger than the new content. Before wrting the new file content, just reset the file lenght to zero.
public static void WriteAllLinesBetter(string path, params string[] lines)
{
if (path == null)
throw new ArgumentNullException("path");
if (lines == null)
throw new ArgumentNullException("lines");
using (var stream = File.OpenWrite(path))
{
stream.SetLength(0);
using (var writer = new StreamWriter(stream))
{
if (lines.Length > 0)
{
for (var i = 0; i < lines.Length - 1; i++)
{
writer.WriteLine(lines[i]);
}
writer.Write(lines[lines.Length - 1]);
}
}
}
}
Upvotes: 4
Reputation: 21
The easiest way for me to do it was usning AppendAllText for last line:
if ($i -ne $lines.Count - 1){
$newLines += $lines[$i]
} else {
$lastLine = $lines[$i]
}
[IO.File]::WriteAllLines($file.FullName, $newLines);
[IO.File]::AppendAllText($file.FullName, $lastLine);
Upvotes: 0
Reputation: 520
To get rid of the trailing newline, the best solution would be, when you read it with .ReadAllLines()
to actually dismiss the last element from your array of strings.
Upvotes: -1
Reputation: 1
i did the same thing just adding a line :3 , hope that helps someone
for(int i = 0; i < Length; i++)
{
line = getLocation(i).X.ToString("0.0", nfi) + ',' + getLocation(i).Y.ToString("0.000", nfi) + ',' + getColorRaw(i).R.ToString("0.000", nfi) + ',' + getColorRaw(i).G.ToString("0.000", nfi) + ',' + getColorRaw(i).B.ToString("0.000", nfi);
writer.Write(line);
if( i < Length - 1) writer.Write("\n");
Upvotes: 0
Reputation: 216313
WriteAllLines writes every single entry in your array and append a newline.
As you can see, every string is on its own line, this means that your last entry is newline terminated and you see a one more line in file. You could prove this with an hexdecimal dump of the file
Looking at the source code of WriteAllLines confirms this.
Internally, it uses the TextWriter.WriteLine(string) method.
Upvotes: 3
Reputation: 27619
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.
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.
Upvotes: 9