如何写入文本文件中的下一行?
如何确保我正在写的消息位于空白行并且不会覆盖更多文本?是否涉及先读取文件?到目前为止,这是我的代码。
Console.WriteLine("What do you wish to write?");
String input2 = Console.ReadLine();
Console.WriteLine("What file do you wish to write to?");
fpath = Console.ReadLine();
StreamWriter sw = new StreamWriter(File.OpenWrite(fpath));
sw.WriteLine(input2);
sw.Dispose();
How can I make sure that the message I'm writing is on a blank line and doesn't write over more text? Does it involve reading the file first? Here is my code so far.
Console.WriteLine("What do you wish to write?");
String input2 = Console.ReadLine();
Console.WriteLine("What file do you wish to write to?");
fpath = Console.ReadLine();
StreamWriter sw = new StreamWriter(File.OpenWrite(fpath));
sw.WriteLine(input2);
sw.Dispose();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StreamWriter
构造函数 具有一个重载,其中包含第二个参数指示是否要附加到文件。此外,此构造函数接受路径,因此您不需要使用File.OpenWrite
。The
StreamWriter
constructor has an overload with a second parameter that indicates whether you want to append to the file. Also, this constructor accepts a path, so you don't need to useFile.OpenWrite
.