问题描述
我有txt文件(65mb),我需要逐行阅读并更改每一行,例如,我有很多行
I have txt file(65mb)i need to read line by line and change each line,For example i have many lines
User=value Password=value Phone=123456789
User=value Password=value Phone=123456789
User=value Password=value Phone=123456789
并且我需要将信用卡/电话的第一个号码更改为*(出于安全原因),并获取像这样的文本并保存,或者只是更改原始文本文件.
and i need to change first number of credit card/Phone to*(security reason), and get text like this and save it, or just to change origin text file.
User=value Password=value Phone=*****6789
User=value Password=value Phone=*****6789
User=value Password=value Phone=*****6789
我建立了新的字符串,并比保存的逐行添加(更改了),但这花了我很多时间,这是我的代码
I build new string and add to there line(changed) by line than save, but it take me to many time this is my code
string NewPath = "";
string lineOfText;
string NewTextFile = "";
using (var filestream = new FileStream(FilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
var file = new StreamReader(filestream, Encoding.UTF8, true, 128);
while ((lineOfText = file.ReadLine()) != null)//here i reading line by line
{
NewTextFile += lineOfText.Substring(0, 124) + "************" +
lineOfText.Substring(136, lineOfText.Length - 136);
NewTextFile += Environment.NewLine;//here i make new string
}
}
NewPath = FilePatharr[1] + "\\temp.txt";
System.IO.File.WriteAllText(NewPath, NewTextFile);//here i save him
有谁知道做这件事的更好的方法,我的代码要花很长时间才能保存这个大文件.
更新
Do any one know better way to do this,my code is taking to long to save this big file.
UPDATE
为什么我对这个问题的回答是-2?这个问题怎么了?我在这里只看到关于如何传递敏感数据的错误答案,以及其他不属于该问题的内容.问题是->快速更改txt文件并保存它的方式
Why do i get -2 for this question? Whats wrong with this question? I see here only wrong answers about how to pass sensitive data and more things that not belong to this questions When the question was -->Fast way to change txt file and save it
通过任何方式,我都能找到如何将文件speedUp从100kb \ sec保存到3MB \ sec的速度,这花了我20秒而不是20分钟的时间
Any way i find out how to do this speed of saving file speedUp from 100kb\sec to 3MB\sec now it taking me 20sec and not 20min like before
推荐答案
您的主要问题是要追加到字符串.这很快就变得昂贵.您应该能够在大约五秒钟内处理该65 MB.这就是我要做的:
Your primary problem here is that you're appending to a string. And that gets expensive very quickly. You should be able to process that 65 MB in about five seconds. Here's what I would do:
string outputFileName = "temp.txt";
using (var outputFile = new StreamWriter(outputFileName))
{
foreach (var line in File.ReadLines(inputFileName))
{
var newLine = line.Substring(0, 124) + "************" +
line.Substring(136, lineOfText.Length - 136);
outputFile.WriteLine(newLine);
}
}
这将比附加字符串快得多.如果您真的想在内存中完成所有操作,请使用StringBuilder
.代替
This is going to be a lot faster than appending strings. If you really want to do it all in memory, then use a StringBuilder
. Instead of
string NewTextFile = "";
使用
StringBuilder NewTextFile = new StringBuilder();
在编写输出时,将字符串连接替换为:
And when you're composing the output, replace the string concatenation with:
NewTextFile.AppendLine(
lineOfText.Substring(0, 124) + "************" +
lineOfText.Substring(136, lineOfText.Length - 136));
最后,将其写入文件:
System.IO.File.WriteAllText(NewPath, NewTextFile.ToString());
这篇关于更改txt文件并将其保存的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!