.NET File.WriteAllLines leaves an empty line at the end of the file

When I save the contents of the String [] array using System.IO.File.WriteAllLines, there is always an empty line at the end of the file. For example:

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

Produces a file (without underscore):

 a b c _ 

There was already such a topic: An empty line in .Net File.WriteAllLines, is this an error? but the author said that "I think something is wrong with my data, that my problem is not WritAllLines", and it was closed as "too localized" (?!?).

This is mistake? How can I get rid of it easily (for now, I just ignore it when reading the file again)?

+9
arrays line
source share
9 answers

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 that was made in the message you are linking to is that when you run ReadAllLines , which considers the string to be the character terminated by a line break. Therefore, when you use the reading method in the file you wrote, you should get the same lines.

If you read the file differently, you will have to deal with linebreaks yourself.

Essentially, you see Expected Behavior.

+8
source share

As others have already pointed out, this is how it works. Here is a method that does this without an additional new line:

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

Using:

 FileExt.WriteAllLinesBetter("test.txt", "a", "b", "c"); 

Writes:

  a enter
 b enter
 c
+14
source share

WriteAllLines writes each record to your array and adds a new line.
As you can see, each line is on a separate line, this means that your last record is completed with a newline, and you see another line in the file. You can prove it with a hex file dump

A search for the WriteAllLines source code confirms this.
Inside, it uses the TextWriter.WriteLine (string) method.

+3
source share

Virtlink's solution is almost perfect. Actually there is a scenario in which you get garbage at the end of the file - when the file exists and its contents are larger than the new content. Before calling the new contents of the file, simply reset the file length 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]); } } } } 
+3
source share

There is a simpler solution:

 // 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); 
+1
source share

I did the same, just adding the line: 3, hope someone help

  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"); 
0
source share

To get rid of the final newline, the best solution would be when you read it with .ReadAllLines() to actually remove the last element from your string array.

0
source share

The easiest way to do this is to use AppendAllText for the 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); 
0
source share

You can also save the file using WriteAllText and attach the array of strings manually, for example:

 File.WriteAllText(file, String.Join("\r\n",correctedLines)); 
0
source share

All Articles