6

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

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

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

one noa
  • 345
  • 1
  • 3
  • 10
user3567884
  • 213
  • 1
  • 6
  • 19
  • I am not.I am downloading this file from CyberArk File Exchange, the Company is making this files and put them there, my goal is to open file remove/change the first(5) number to ******6789 – user3567884 May 26 '14 at 13:04
  • Instead of buffering NewTextFile into a string, you should write it in new text file and rename it after. You will avoid losing your data if your application is killed at `File.WriteAllText` – Perfect28 May 26 '14 at 13:05
  • [This](http://stackoverflow.com/a/4274051/1029287) answers how to read and handle a large file, writing it back should be trivial. – void May 26 '14 at 13:10
  • When i said Company i did't mean that is my company, it is customer Company they send to as files like this – user3567884 May 26 '14 at 13:17

4 Answers4

3

You could keep WriteAllLines and use ReadAllLines:

  1. ReadAllLines to get all lines as an array of strings
  2. Enumerate the array and use string.Replace to replace its content, line by line
  3. WriteAllLines and overwrite existing file

Or you could use stream reader/writer and read/write line by line. You currently are building a string with millions of concatenations, that's the performance issue.

That being said, you probably have an obvious security issue here. Why was critical information such as credit card numbers stored as plain text in the first place?

ken2k
  • 48,145
  • 10
  • 116
  • 176
3

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);
    }
}

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 = "";

Use

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));

Finally, to write it to the file:

System.IO.File.WriteAllText(NewPath, NewTextFile.ToString());
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0
 public static string ChangeCardNumber(string FilePath, string txtFileName)
        {
            string NewPath = "";
            string lineOfText;
            string NewTextFile = "";
            NewPath = FilePatharr[0] + "\\temp_" + txtFileName;

            using (var filestream = new FileStream(FilePath,
                                 FileMode.Open,
                                 FileAccess.Read,
                                 FileShare.ReadWrite))
            {
                var file = new StreamReader(filestream, Encoding.UTF8, true, 128);
                System.IO.File.Create(NewPath).Close();
                int res = 0;
                while ((lineOfText = file.ReadLine()) != null)
                {
                    res++;

                    if (!lineOfText.Contains("EOF"))
                    {
                        NewTextFile += lineOfText.Substring(0, 124) + "************" +
                        lineOfText.Substring(136, lineOfText.Length - 136);
                        NewTextFile += Environment.NewLine;
                    }
                    if (res % 200 == 0 || lineOfText.Contains("EOF"))//check if endline and if in NewTextFile 200lines
                    {
                        System.IO.File.AppendAllText(NewPath, NewTextFile);
                        NewTextFile = "";
                    }
                }

            }



            return NewPath;

        }
user3567884
  • 213
  • 1
  • 6
  • 19
-6

Well I Guess You Could Do It Another Way And This Way Is The Way I Use For All of my saving needs. if you want to save it all at the same time then this would work! or you could have it loaded in VARIABLES and then save it but this is the code to save it. it is very simple and easy to edit

There You Go

Basic Code To Add A New Line With Text

echo your text here >>filename.txt

For Explaination Please Visit The DropBox Link: https://www.dropbox.com/s/nufkecx1b8088cu/HOW%20TO%20USE%20THIS.txt

user3674709
  • 133
  • 1
  • 2
  • 2
    One this isn't c# and two don't include links to your personal dropbox in answers, it will become a dead link in the future and it more security questions on top of those from the original question, oh and #3 it doesn't answer the question – Sayse May 26 '14 at 13:19