我使用c#计算出大量的点(x,y,z),现在想将这些点写进一个记事本文件*.txt。
需要循环写进,但是不能覆盖上一次的数据,应该怎么写程序代码?由于点数据是不同的类别,需要写进文档的时候自己再额外的写进一些标记加以区分,比如写A、B、C,后面跟着数据。谢...
需要循环写进,但是不能覆盖上一次的数据,应该怎么写程序代码?由于点数据是不同的类别,需要写进文档的时候自己再额外的写进一些标记加以区分,比如写A、B、C,后面跟着数据。谢谢了。
展开
1个回答
展开全部
追加写入就行了,键肆方式有很多,比如,至于你怎么区分那就随你的了,比如全用A,x,y,z这种格式,读取的稿指轿时候读出一行后用split切割就行了。
The File class provides three methods to append an existing file.
AppendText
AppendAllText
AppendAllLines
The AppendText method creates a StreamWriter object that appends UTF-8 encoded text to an existing text file.
string fileName = @"C:\Temp\Mahesh.txt";
//示例
int x=3,y=5,z=7;
using (StreamWriter sw =File.AppendText(fileName))
{
sw.WriteLine(string.Format("{0},{1},{2},{3}"逗烂,"A",x,y,z));//示例
sw.WriteLine("--------- Append Text Start ----------");
sw.WriteLine("New author started");
sw.WriteLine("a book on Files Programming ");
sw.WriteLine("using C#");
sw.WriteLine("--------- Append Text End ----------");
}
The AppendAllText method opens a file, appends a specified string to the file and then closes the file. If the file does not exist, this method creates a new file, writes the specified string to the file, and then closes the file.
string fileName = @"C:\Temp\Mahesh.txt";
string appendText = "Append all text method ----";
File.AppendAllText(fileName, appendText);
File.AppendAllText(fileName,"Append some more text");
File.AppendAllText(fileName,"Append all text end");
// Read all text
string readText = File.ReadAllText(fileName);
Console.WriteLine(readText);
The AppendAllLines method appends lines to a file and then closes the file.
// We can read a line from one file and write to other file
string fileName = @"C:\Temp\Mahesh.txt";
string[] lines = File.ReadAllLines(fileName);
// Write all lines to a new file
string outputFileName = @"C:\temp\WriteLinesFile.txt";
File.WriteAllLines(outputFileName, lines);
// Append more lines
string[] moreLines = new string[]{"Line 1 goes here", "Line 2 goes here" };
File.AppendAllLines(outputFileName, moreLines);
// Read all text
string readText = File.ReadAllText(outputFileName);
Console.WriteLine(readText);
The File class provides three methods to append an existing file.
AppendText
AppendAllText
AppendAllLines
The AppendText method creates a StreamWriter object that appends UTF-8 encoded text to an existing text file.
string fileName = @"C:\Temp\Mahesh.txt";
//示例
int x=3,y=5,z=7;
using (StreamWriter sw =File.AppendText(fileName))
{
sw.WriteLine(string.Format("{0},{1},{2},{3}"逗烂,"A",x,y,z));//示例
sw.WriteLine("--------- Append Text Start ----------");
sw.WriteLine("New author started");
sw.WriteLine("a book on Files Programming ");
sw.WriteLine("using C#");
sw.WriteLine("--------- Append Text End ----------");
}
The AppendAllText method opens a file, appends a specified string to the file and then closes the file. If the file does not exist, this method creates a new file, writes the specified string to the file, and then closes the file.
string fileName = @"C:\Temp\Mahesh.txt";
string appendText = "Append all text method ----";
File.AppendAllText(fileName, appendText);
File.AppendAllText(fileName,"Append some more text");
File.AppendAllText(fileName,"Append all text end");
// Read all text
string readText = File.ReadAllText(fileName);
Console.WriteLine(readText);
The AppendAllLines method appends lines to a file and then closes the file.
// We can read a line from one file and write to other file
string fileName = @"C:\Temp\Mahesh.txt";
string[] lines = File.ReadAllLines(fileName);
// Write all lines to a new file
string outputFileName = @"C:\temp\WriteLinesFile.txt";
File.WriteAllLines(outputFileName, lines);
// Append more lines
string[] moreLines = new string[]{"Line 1 goes here", "Line 2 goes here" };
File.AppendAllLines(outputFileName, moreLines);
// Read all text
string readText = File.ReadAllText(outputFileName);
Console.WriteLine(readText);
追问
亲,你写的是英文注释啊,我理解的不太好,能不能提供一个例子?谢谢
追答
你不用看说明啊,只看代码就行了,第一段就是追加的例子
string fileName = @"C:\Mahesh.txt";//要操作的文件为C盘下的Mahesh.txt
int x=3,y=5,z=7;//示例
using (StreamWriter sw =File.AppendText(fileName))
{
sw.WriteLine(string.Format("{0},{1},{2},{3}","A",x,y,z));//示例
}
运行之后C盘的Mahesh.txt下就会多出一行A,3,5,7
后面那个是将自定义的一个moreLines数组中的所有数据都添加到文件中
string outputFileName = @"C:\WriteLinesFile.txt";
string[] moreLines = new string[]{"Line 1 goes here", "Line 2 goes here" };
File.AppendAllLines(outputFileName, moreLines);
//之后WriteLinesFile.txt中就会多出两行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询