Home > Net >  StringBuilder string written text files, always a blank line
StringBuilder string written text files, always a blank line

Time:11-13

 
StringBuilder sb=new StringBuilder();

Sb. AppendLine ($" first line ");

Foreach (Mapping item in MapData)
{
Sb. AppendLine (item. The ToString ());
}

//store
String DicFilePath=Path.Com bine (BaseInfo ProjectName, ProjectName + "_Dic. TXT");
If (the File. The Exists (DicFilePath) File. Delete (DicFilePath);
Var fs=new FileStream (DicFilePath, FileMode. Append, FileAccess. Write FileShare. ReadWrite);
Using (StreamWriter sw=new StreamWriter (fs, Encoding GetEncoding (" gb2312 ")))
{
Sw. Write (sb. ToString ());
}
Fs. The Close ();


The final text file will always be a blank line, and more consult how to solve?

CodePudding user response:

 
Appendline lines end with a newline, append not
Sb. AppendLine (item. The ToString ());
Sb. Append (item. The ToString ());

CodePudding user response:

Append Line you require Line ah,

CodePudding user response:

refer to the second floor is nu month god reply:
Append Line are you require a newline,


reference 1/f, kill matt, bull response:
 
Appendline lines end with a newline, append not
Sb. AppendLine (item. The ToString ());
Sb. Append (item. The ToString ());


I don't understand, sb. Append (" content "); ,
This sentence is not start a new line should be written to two words?
But the effect is written to start a new line after the "content" two characters

CodePudding user response:

 sw. Write (sb. ToString (). TrimEnd (new char [] {\ 'r', '\ n'})); 

Perfect solution, two teachers ~ ~, thank you

CodePudding user response:

AppendLine is an extra line breaks after
Append just add
 
StringBuilder sb=new StringBuilder();
Sb. Append (" 123 ");
Sb. AppendLine (" 456 ");
Sb. Append (" 789 ");

Results:
123456
789

CodePudding user response:

Logical error:

 sb. Append ($" first line "); 

Foreach (Mapping item in MapData)
{
Sb. AppendLine ();
Sb. Append (item. The ToString ());
}

CodePudding user response:

refer to 6th floor interacting in a professional developer response:
logic error:

 sb. Append ($" first line "); 

Foreach (Mapping item in MapData)
{
Sb. AppendLine ();
Sb. Append (item. The ToString ());
}


See your code, the feeling is good simple ah
My mind is to what, all of this thought...

CodePudding user response:

If you can see what you said on the # 3 floor logic with you write their own code does not "unity" will be better,
  •  Tags:  
  • C#
  • Related