0

我的问题是关于从文本文件中获取持续数据。这是问题的描述:

我的要求是将每一行保存在一个文本文件中。此文本文件由外部来源以未知的时间间隔提供。此外,最重要的是 - 当文件达到 100 行(例如)时,它会将这些行存档在无法访问的源中并清除其内容。

考虑一个监听器查看文件并看到它包含 10 行的情况,监听器将获取这些行并保存它们。然后在增量时间(例如 1 秒)之后,它再次查看并看到 93 行。它还保存了未录制的 83 个。现在,当听众第三次偷看时,它会看到 3 行。它显然会保存它们,但请注意有 7 行(索引为 94-100)没有被听众记录下来。

看起来偷看恒定的增量并不能确保不会发生失误。

您如何建议实现这一要求的最佳方式?

4

3 回答 3

0

您可以将行保存在某种形式的集合中,例如并使用 while 或 if 语句List<string>验证集合的容量(例如 100),List1.Add(line)while(count < capactity)

如果您对文件具有读/写访问权限,则可以在将这些行添加到集合后删除它们。

于 2013-03-21T06:16:31.583 回答
0

这是从文件中逐行获取数据并将其保存到数据库的代码>>

 string line;
            string[] subLine;
            DialogResult result = openFileDialog1.ShowDialog();
            try
            {
                if (result == DialogResult.OK)
                {
                    string file = openFileDialog1.FileName;
                    StreamReader inputData = new StreamReader(file);
                    const int maxRecords = 500;
                    int actualRecords = 0;
                    while ((line = inputData.ReadLine()) != null)
                    {
                        subLine = line.Split(',');

                        try
                        {


                            string unknown = "unknown Fileld";
                            con.Open();
                            // int j=subLine.Length;
                            //for(int i=0;i<subLine.Length;i++)
                            if (actualRecords < maxRecords)
                            {
                                int i = 0;
                                //int j=subLine.Length;
                                //int index=subLine.Length-j;

                               // SQL= "insert into tradeFile values('" + ID + "','" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')";

                                cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                // cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                cmd.ExecuteNonQuery();
                                actualRecords++;
                                //  j--;

                            }
                            else
                            {
                                int i = 0;
                                //int j=subLine.Length;
                                //int index=subLine.Length-j;
                                cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                //cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con);
                                cmd.ExecuteNonQuery();
                                actualRecords = 0;
                            }





                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            con.Close();
                        }
                        //counter++;
                    }
                    inputData.Close();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

希望您能应用进一步的逻辑,这将对您有所帮助。

于 2013-03-21T06:05:56.933 回答
0

更多不同文件操作详情点击这里

C#中的文件操作

于 2013-03-21T06:10:38.110 回答