在一个文本文件中的特定位置添加一个新行。 [英] Add a new line at a specific position in a text file.
问题描述
我想添加文本的特定行的文件。 。特别是两个边界之间
I am trying to add a specific line of text in a file. Specifically between two boundaries.
它会是什么样子,如果我想在加item1的边界之间的一条线的一个例子:
An example of what it would look like if I wanted to add a line in between the boundaries of 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]
这是我到目前为止已经试过,但它远不正确。它口口声声说该文件正在使用的读卡器,因此不能由作家进行编辑,当我没有得到它的工作,它清除了整个文档。
This is what I have tried so far, however Its nowhere near correct. It keeps saying that the file is being used by the reader so it cant be edited by the writer, when I did get it to work it cleared the entire document.
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");
}
}
我也想知道怎么写的行该文件的结尾。
I would also like to know how to write lines at the end of the document.
推荐答案
这将增加您想要的位置就行了。 (请确保您有使用System.IO;
添加)
This will add the line where you want it. (Make sure you have using System.IO;
added)
public void createEntry(String npcName)
{
//npcName = @"[/item1]"; <-- note the '/'.
string lineToAdd = "//Add a line here in between the specific boundaries";
string fileName = "test.txt";
List<string> txtLines = new List<string>();
//Fill a List<string> with the lines from the txt file.
foreach (string str in File.ReadAllLines(fileName))
{
txtLines.Add(str);
}
//Insert the line you want to add last under the tag 'item1'.
txtLines.Insert(txtLines.IndexOf(npcName), lineToAdd);
//Clear the file. The using block will close the connection immediately.
using (File.Create(fileName)) { }
//Add the lines including the new one.
foreach (string str in txtLines)
{
File.AppendAllText(fileName, str + Environment.NewLine);
}
}
两年UPDATE:这是我最高的投票答案,但我两年前写的,它可以使用一些改进。我正在做一些奇怪的东西,在上面的方法,我想为未来的读者一个更好的选择。
TWO YEAR UPDATE: This is my highest voted answer but I wrote it two years ago and it could use some improvements. I'm doing some strange things in the above method and I want to provide a better alternative for future readers.
public void CreateEntry(string npcName) //npcName = "item1"
{
var fileName = "test.txt";
var endTag = String.Format("[/{0}]", npcName);
var lineToAdd = "//Add a line here in between the specific boundaries";
var txtLines = File.ReadAllLines(fileName).ToList(); //Fill a list with the lines from the txt file.
txtLines.Insert(txtLines.IndexOf(endTag), lineToAdd); //Insert the line you want to add last under the tag 'item1'.
File.WriteAllLines(fileName, txtLines); //Add the lines including the new one.
}
这篇关于在一个文本文件中的特定位置添加一个新行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!