C# 在中读取.dat文件

C# 在中读取.dat文件,c#,console-application,C#,Console Application,您好,我目前正在尝试阅读一个.Dat,我有一些麻烦 .Date文件数据的格式如下所示 static void Main(string[] args) { StreamReader tbal = new StreamReader("C:\\Users\\pbrady\\Desktop\\Code\\PNLSummary\\PNLSummary\\bin\\Debug\\tbal.dat", System.Text.Encoding.Default); s

您好,我目前正在尝试阅读一个.Dat,我有一些麻烦

.Date文件数据的格式如下所示

static void Main(string[] args)
    {
         StreamReader tbal  = new StreamReader("C:\\Users\\pbrady\\Desktop\\Code\\PNLSummary\\PNLSummary\\bin\\Debug\\tbal.dat", System.Text.Encoding.Default);
        string contents = tbal.ReadToEnd().Trim();
        string[] split = System.Text.RegularExpressions.Regex.Split(contents, "|", RegexOptions.None);
        Console.WriteLine("*****LOADING*****");
        foreach (string s in split)
        {
            using (StreamWriter writer = new StreamWriter("C:\\Users\\pbrady\\Desktop\\Code\\PNLSummary\\PNLSummary\\bin\\Debug\\Test.txt", true))
            {
                writer.WriteLine(s);
                Console.WriteLine(s);

            }
        }
        Console.WriteLine("*****Complete*****");
    }
62720 |电话| 1000.17 | p | 31040.94 | 30154.88 | 250

我想做的是将|之间的每个项目存储到单独的字符串中

我有以下代码,当我运行它时,输出到一个文本文件,每个字符都在一个换行符上。乙二醇

六,

二,

七,

二,

0

我的代码如下

static void Main(string[] args)
    {
         StreamReader tbal  = new StreamReader("C:\\Users\\pbrady\\Desktop\\Code\\PNLSummary\\PNLSummary\\bin\\Debug\\tbal.dat", System.Text.Encoding.Default);
        string contents = tbal.ReadToEnd().Trim();
        string[] split = System.Text.RegularExpressions.Regex.Split(contents, "|", RegexOptions.None);
        Console.WriteLine("*****LOADING*****");
        foreach (string s in split)
        {
            using (StreamWriter writer = new StreamWriter("C:\\Users\\pbrady\\Desktop\\Code\\PNLSummary\\PNLSummary\\bin\\Debug\\Test.txt", true))
            {
                writer.WriteLine(s);
                Console.WriteLine(s);

            }
        }
        Console.WriteLine("*****Complete*****");
    }
试试这个

static void Main(string[] args)
    {
        StreamReader tbal = new StreamReader("tbal.dat");
        string contents = tbal.ReadToEnd().Trim();
        string[] split = contents.Split('|');
        Console.WriteLine("*****LOADING*****");
        StreamWriter writer = new StreamWriter("Test.txt", true);
        foreach (string s in split)
        {
            writer.WriteLine(s);
            Console.WriteLine(s);
        }

        tbal.Close();
        writer.Close();
        Console.WriteLine("*****Complete*****");
    }

无需提供完整路径。这些文件将从应用程序的调试文件夹中创建和读取。

是否是要拆分的有效正则表达式模式?为什么不使用
contents.Split(“|”)
?是否要在新文件的新行中写入每个字符串?我想要的是在单个数据类型中的|之间包含每个项目,以便我可以对它们进行计算。您使用了
Regex.Split
作为
string.Split
,这就是为什么您的代码不能提供您想要的内容
string.Split
(如您得到的答案中所建议的),需要给定的字符/字符串(在本例中为“|”)
Regex.Split
需要一个模式
System.Text.RegularExpressions.Regex.Split(内容,“|”,RegexOptions.None)将“|”分析为一个模式,而不是单个字符。为什么不从该数据创建一个数据表,并以闪电般的速度计算呢?;)
        static void Main(string[] args)
        {
          String myfile= System.IO.File.ReadAllText("myfile.dat");
          String[] strWord = myfile.Split('|');
          using(System.IO.StreamWriter writer = new System.IO.StreamWriter("myfile.txt", true))
          {              
          foreach (string str in strWord)
              writer.WriteLine(str.Trim());
          }
        }