【发布时间】:2023-08-07 05:34:01
【问题描述】:
我正在尝试在文件中添加特定的文本行。特别是在两个边界之间。
如果我想在 item1 的边界之间添加一条线会是什么样子的示例:
[item1]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
//Add a line here in between the specific boundaries
[/item1]
[item2]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 8
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item2]
[item3]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item3]
这是我迄今为止尝试过的,但它远非正确。它一直说该文件正在被阅读器使用,因此作者无法编辑它,当我让它工作时它清除了整个文档。
public void createEntry(String npcName)
{
String line;
String fileName = "Drops.de";
StreamWriter streamWriter = new StreamWriter(fileName);
StreamReader streamReader = new StreamReader(fileName);
line = streamReader.ReadLine();
if (line == ("[" + npcName + "]"))
{
streamReader.ReadLine();
streamWriter.WriteLine("Test");
}
}
我也想知道如何在文档末尾写行。
【问题讨论】:
-
像上一个问题一样读取文件,并在添加新行的同时写入新文件。
-
我添加了我尝试过的,我认为到目前为止还很遥远
标签: c# io streamwriter