.Net文件写入和字符串拆分问题

时间:2012-12-07 04:54:56

标签: c# .net

我有一个要求,即使用给定字符拆分文件。 默认拆分选项是CRLF,LF和CR。

在这些情况下,我将分别用\ r \ n和\ n和\ r分割线。

此外,我还要求处理任何大小的文件。 (处理基本上是将给定字符串插入给定位置的文件中)。

为此,我正在以1024字节的块读取文件。

然后我应用string.Split()方法。 Split()方法提供了忽略空格和无空格的选项。

我必须将这些换行符添加回该行。 为此,我使用二进制编写器,我正在将字节数组写入新文件。 问题: -

1)当换行符为CRLF,并且拆分选项为NONE时,也会在拆分数组中添加空格。给出第二个选项(忽略空格)CRLF正常工作。

2)位忽略空格选项会产生其他问题,因为我逐字节地读取文件,我不能忽略空格。

3)当换行符不是默认值时(例如“|”,空值前置于结果行。

任何人都可以解决我的问题吗?

这是我写的方法

    private static void ProcessOriginalFile(string filePath, string destinationFilePath, int fromPosition, int toPosition, string lineBreakCharacter, string replaceString, long fileSize, bool ignoreHeader, bool ignoreFooter)
    {
        int chunkSize = fileSize < 1024 ? (int)fileSize : 1024;//bytes
        byte[] chunkData = new byte[chunkSize];
        char[] charactersSeparator=new char[1];

        charactersSeparator = CommonOperations.ResolveTheRecordBreak(lineBreakCharacter);           

        int totalLineBreakCharactersLength = 0;
        for (int i = 0; i < charactersSeparator.Length; i++)
        {
            if (charactersSeparator[i] == 0)
                break;
            totalLineBreakCharactersLength = totalLineBreakCharactersLength + BitConverter.GetBytes(charactersSeparator[i]).Length;
        }


            using (BinaryReader fileReader = new  BinaryReader(new FileStream(filePath, FileMode.Open)))
            {
                string lastChunk = string.Empty;
                string chunkcontents = string.Empty;
                IList<byte[]> dataToBeWritten = new List<byte[]>();

                while (fileSize > 0)
                {
                    chunkData = fileReader.ReadBytes(chunkSize);
                    byte[] chunkToBeWritten = new byte[chunkSize];
                    chunkcontents = chunkcontents + System.Text.ASCIIEncoding.UTF8.GetString(chunkData);

                    string[] splittedArray = new string[1];
                        splittedArray = chunkcontents.Split(charactersSeparator, StringSplitOptions.RemoveEmptyEntries);
                        if (ignoreHeader)
                        {
                            splittedArray = splittedArray.Skip(1).ToArray();
                            ignoreHeader = false;
                            if (splittedArray.Count() == 0)
                                continue;
                        }

                    int count = 0;
                    if (splittedArray != null || splittedArray.Count() > 0)
                    {
                        foreach (string str in splittedArray)
                        {
                            ++count;
                            if (count != splittedArray.Length)
                            {
                                string stringToBeEdited = string.Empty;
                                stringToBeEdited = str;
                                if (stringToBeEdited.Length < (toPosition + 1))
                                    throw new Exception("Position exceeds the string length. Line contents are : " + stringToBeEdited + " String length is : " + stringToBeEdited.Length + " To Position is : " + toPosition);
                                //replace the text between from and two positions with the replaced string
                                stringToBeEdited = stringToBeEdited.Remove(fromPosition, toPosition + 1 - fromPosition).Insert(fromPosition, replaceString);

                                //clear the array
                                dataToBeWritten.Clear();
                                AddLineBreakCharacter(lineBreakCharacter, charactersSeparator, totalLineBreakCharactersLength, dataToBeWritten, ref chunkToBeWritten, ref stringToBeEdited);

                                //write data using binary writer
                                WriteBinaryDataToFile(destinationFilePath, dataToBeWritten);
                                totalLinesProcessed++;
                            }
                            else
                            {
                                lastChunk = str;
                                chunkcontents = str;
                            }
                        }
                    }
                    fileSize = fileSize - chunkSize;

                    /*if file size is less than the chunksize*2, then chunk size should be the file size 
                     * and chunkdata array length should be that of the file size*/

                    if (fileSize < (chunkSize * 2))
                    {
                        chunkSize = (int)fileSize;
                        chunkData = new byte[chunkSize];
                    }
                }


                if (!string.IsNullOrEmpty(lastChunk))
                {
                    if (ignoreFooter == false)
                    {
                        if (lastChunk.Length >= toPosition + 1)
                            lastChunk = lastChunk.Remove(fromPosition, toPosition + 1 - fromPosition).Insert(fromPosition, replaceString);
                        else
                            throw new Exception("Position exceeds in the last line of the file. Line contents are : " + lastChunk + " String length is : " + lastChunk.Length + " To Position is : " + toPosition);
                        dataToBeWritten.Clear();
                        byte[] chunkToBeWritten = new byte[chunkSize];
                        AddLineBreakCharacter(lineBreakCharacter, charactersSeparator, totalLineBreakCharactersLength, dataToBeWritten, ref chunkToBeWritten, ref lastChunk);
                        WriteBinaryDataToFile(destinationFilePath, dataToBeWritten);
                        totalLinesProcessed++;
                    }
                }
            }
    }

    private static void WriteBinaryDataToFile(string destinationFilePath, IList<byte[]> chunkToBeWritten)
    {
            using (FileStream fileSream = new FileStream(destinationFilePath, FileMode.Append))
            {
                using (BinaryWriter outfile = new BinaryWriter(fileSream, Encoding.ASCII))
                {
                    foreach (byte[] item in chunkToBeWritten)
                    {
                        outfile.Write(item);
                    }
                }
            }
    }

    private static void AddLineBreakCharacter(string lineBreakCharacter, char[] charactersSeparator, int totalLineBreakCharactersLength, IList<byte[]> dataToBeWritten, ref byte[] chunkToBeWritten, ref string stringToBeEdited)
    {
        switch (lineBreakCharacter)
        {
            case CommonConstants.NEW_LINE:
                stringToBeEdited = stringToBeEdited + CommonConstants.CARRIAGE_RETURN_CHARACTER + CommonConstants.NEW_LINE_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);
                break;

            case CommonConstants.LINE_FEED:
                stringToBeEdited = stringToBeEdited + CommonConstants.NEW_LINE_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                break;
            case CommonConstants.CARRIAGE_RETURN:
                stringToBeEdited = stringToBeEdited + CommonConstants.CARRIAGE_RETURN_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                break;
            default:
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                byte[] seperatorCharArray = new byte[totalLineBreakCharactersLength];
                int destinationIndex = 0;
                foreach (char ch in charactersSeparator)
                {
                    if (ch == 0)
                        break;
                    //convert the character to byte array
                    byte[] charArr = BitConverter.GetBytes(ch);

                    //copy character bytes to seperator character array                        
                    Array.Copy(charArr, 0, seperatorCharArray, destinationIndex, charArr.Length);
                }
                //add line break characters array to list
                dataToBeWritten.Add(seperatorCharArray);
                break;
        }
    }

2 个答案:

答案 0 :(得分:0)

您可以使用string.Replace并在分割

之前将问题符号替换为""

答案 1 :(得分:0)

我已经解决了这个问题。我只是转储上面的代码并使用TextReader逐个字符地读取文件。同时我检查了换行符。我一收到它,就处理了这条线。