windows 将新行追加到文件的最简洁/最短的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8545880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The most condensed / shortest way to append a new line to a file
提问by James Teare
There are various method of appending text to files, although I was wondering if any knows the shortest way to do this
有多种将文本附加到文件的方法,尽管我想知道是否有任何人知道这样做的最短方法
e.g. Add a new line to an existing txt file:
例如,在现有的 txt 文件中添加一个新行:
line1
line2
<new-line-here>
回答by driis
That would be something like:
那将是这样的:
File.AppendAllText("c:\filepath.txt", "text to append");
See File.AppendAllTextfor details. The Fileclass has a lot of useful static methods for doing common file operations.
有关详细信息,请参阅File.AppendAllText。该文件类有很多做普通的文件操作有用的静态方法。
回答by M.Babcock
The File class's static methods make it fairly straight forward:
File 类的静态方法使它相当简单:
File.AppendAllLines("filename.txt", new string[] { "text to append" });
Edit:Using an array is slightly shorter.
编辑:使用数组略短。
回答by Ry-
System.IO.File.AppendAllText("some file", Environment.NewLine);
Is that what you mean?
你是这个意思吗?
回答by Niklas
System.IO.File.AppendAllText(@"c:\test.txt",Environment.NewLine);
Is the right way to do it, just \n wont do it !
是正确的方法,只是\n 不会这样做!
回答by Kashif Khan
Try this code
试试这个代码
StreamWriter sw = File.AppendText(file_path);
sw.WriteLine("appended text");
Source = http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx
来源 = http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx