读取文件,检查列的正确性,写入文件C#

时间:2015-05-27 16:16:58

标签: c# visual-studio file-io delimiter

我需要检查某些数据列,以确保没有尾随空格。起初以为我觉得这很容易,但在试图达到目标之后我就陷入了困境。

我知道我需要检查的列中应该有6位数字。如果有更少,我会拒绝,如果有更多,我会修剪空白。在为整个文件执行此操作之后,我想将其写回具有相同分隔符的文件。

这是我的尝试:

除了编写文件外,其他所有内容似乎都正常工作。

if (File.Exists(filename))
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    string lines = sr.ReadLine();
                    string[] delimit = lines.Split('|');

                    while (delimit[count] != "COLUMN_DATA_TO_CHANGE")
                    {
                        count++;
                    }

                    string[] allLines = File.ReadAllLines(@filename);

                    foreach(string nextLine in allLines.Skip(1)){
                        string[] tempLine = nextLine.Split('|');
                        if (tempLine[count].Length == 6)
                        {
                            checkColumn(tempLine);
                            writeFile(tempLine);
                        }
                        else if (tempLine[count].Length > 6)
                        {
                            tempLine[count] = tempLine[count].Trim();
                            checkColumn(tempLine);                               
                        }
                        else
                        {
                            throw new Exception("Not enough numbers");
                        }
                    }
                }
            }
        } 

 public static void checkColumn(string[] str)
    {
        for (int i = 0; i < str[count].Length; i++)
        {
            char[] c = str[count].ToCharArray();
            if (!Char.IsDigit(c[i]))
            {
                throw new Exception("A non-digit is contained in data");
            }
        }
    }

    public static void writeFile(string[] str)
    {
        string temp;
        using (StreamWriter sw = new StreamWriter(filename+ "_tmp", false))
        {
            StringBuilder builder = new StringBuilder();
            bool firstColumn = true;

            foreach (string value in str)
            {
                if (!firstColumn)
                {
                    builder.Append('|');
                }
                if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
                {
                    builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
                }
                else
                {
                    builder.Append(value);
                }
                firstColumn = false;
            }
            temp = builder.ToString();
            sw.WriteLine(temp);
        }
    }

如果有更好的方法可以解决这个问题,我很乐意听到。感谢您查看此问题。

编辑: 文件结构 -

国|姓名|姓氏| uniqueID(我正在检查的列)|地址|等

USA | John | Doe | 123456 | 5 main street |

注意6

之后的空格

2 个答案:

答案 0 :(得分:2)

var oldLines = File.ReadAllLines(filePath):
var newLines = oldLines.Select(FixLine).ToArray();
File.WriteAllLines(filePath, newLines);

string FixLine(string oldLine)
{
    string fixedLine = ....
    return fixedLine;
}

答案 1 :(得分:1)

编写文件的主要问题是你打开每个输出行的输出文件,然后用append = false打开它,这会导致每次都覆盖文件。更好的方法是打开输出文件一次(可能在验证输入文件头之后)。

另一个问题是您使用.ReadAllLines()再次打开输入文件。最好在循环中一次读取一行现有文件。

考虑这个修改:

using (StreamWriter sw = new StreamWriter(filename+ "_tmp", false))
{
    string nextLine;

    while ((nextLine = sr.ReadLine()) != null)
    {
        string[] tempLine = nextLine.Split('|');
        ...
        writeFile(sw, tempLine);
相关问题