Example #1
0
        public void RemoveLinesAndSpacesFromFile(string line, string filePath)
        {
            FileInfoAsPerLine fileInfo = ReadFileAndCheckForLineOfString(filePath, line);

            fileInfo.LinesInFile = RemoveLinesAndSpacesFromFileInternal(fileInfo.LinesInFile, fileInfo.LineNumbersOfMatchedLine, fileInfo.LinesContainingUnnecessarySpace);
            WriteFile(fileInfo, overwriteFile: true, removeLineFromFile: true);
        }
Example #2
0
        public void WriteLineToFile(string line, string filePath)
        {
            FileInfoAsPerLine fileInfo = ReadFileAndCheckForLineOfString(filePath);

            if (fileInfo.FileDoesExist)
            {
                OverwriteFile(fileInfo.FilePath, line);
                WriteFile(fileInfo);
            }
        }
Example #3
0
        private void WriteFile(FileInfoAsPerLine fileInfo, bool overwriteFile = false, bool removeLineFromFile = false)
        {
            if (fileInfo.FileDoesExist)
            {
                if (overwriteFile)
                {
                    OverwriteFile(fileInfo.FilePath);
                }

                for (int i = 0; i < fileInfo.LinesInFile.Count; i++)
                {
                    AppendToTheFile(fileInfo.FilePath, fileInfo.LinesInFile[i]);
                }
            }
        }
Example #4
0
        public FileInfoAsPerLine ReadFileAndCheckForLineOfString(string filePath, string matchLine = "", bool decomposeIntoWords = false)
        {
            int lineNumber = 1;

            FileInfoAsPerLine fileInfo = new FileInfoAsPerLine();

            fileInfo.FilePath    = filePath;
            fileInfo.LineToMatch = matchLine;

            if (File.Exists(filePath))
            {
                fileInfo.FileDoesExist = true;
                StreamReader objReader = new StreamReader(filePath, false);
                int          spaces    = 0;

                do
                {
                    //Reading line
                    string line = objReader.ReadLine();
                    //Seprating words of a line and storing them

                    if (string.IsNullOrEmpty(line))
                    {
                        spaces++;
                    }
                    else
                    {
                        if (spaces > 2)
                        {
                            for (int i = spaces - 1; i > 0; i--)
                            {
                                fileInfo.LinesContainingUnnecessarySpace.Add(lineNumber - i);
                            }
                        }
                        spaces = 0;
                    }

                    if (decomposeIntoWords)
                    {
                        fileInfo.AddWords(line);
                    }
                    //Checking for matched file
                    if (line.Equals(matchLine))
                    {
                        fileInfo.HasLine = true;
                        fileInfo.LineNumbersOfMatchedLine.Add(lineNumber);
                    }
                    //line += Environment.NewLine;
                    //Adding line to list of lines in the file
                    fileInfo.LinesInFile.Add(line);
                    lineNumber++;
                }while (objReader.Peek() != -1);

                objReader.Close();
            }
            else
            {
                fileInfo.FileDoesExist = false;
                Debug.Log("File does not exist");
            }

            return(fileInfo);
        }