如何将从外部文件加载的数据保存到程序中?

发布于 2024-12-28 05:07:16 字数 1376 浏览 3 评论 0原文

基于下面的函数,它用于从文件.dat加载数据。问题是每次加载新文件时,以前的文件都会被覆盖。如何在程序中存储前一个文件的数据,以便加载新文件时,新数据将添加到前一个文件中?

private void btnLoad_Click(object sender, EventArgs e)
{
    try
    {

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Data File (*.dat)|*.dat";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            TextReader f = new StreamReader(openFileDialog1.FileName);

            String line;

            this.letterData.Clear();
            this.letters.Items.Clear();

            while ((line = f.ReadLine()) != null)
            {
                int sampleSize = Form1.DOWNSAMPLE_HEIGHT * Form1.DOWNSAMPLE_WIDTH;
                char ch = char.ToUpper(line[0]);
                bool[] sample = new bool[sampleSize];

                int idx = 2;
                for (int i = 0; i < sampleSize; i++)
                {
                    if (line[idx++] == '1')
                        sample[i] = true;
                    else
                        sample[i] = false;
                }

                this.letterData.Add(ch, sample);
                this.letters.Items.Add("" + ch);
            }

            f.Close();
        }
        MessageBox.Show(this, "File Loaded");

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

Based on the function below, it is used to load data from file .dat. The problem is every time i load a new file, the previous file will be overwritten. how to store the data from previous file inside the program so that when loading a new file, the new data will be added to the previous one?

private void btnLoad_Click(object sender, EventArgs e)
{
    try
    {

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "Data File (*.dat)|*.dat";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            TextReader f = new StreamReader(openFileDialog1.FileName);

            String line;

            this.letterData.Clear();
            this.letters.Items.Clear();

            while ((line = f.ReadLine()) != null)
            {
                int sampleSize = Form1.DOWNSAMPLE_HEIGHT * Form1.DOWNSAMPLE_WIDTH;
                char ch = char.ToUpper(line[0]);
                bool[] sample = new bool[sampleSize];

                int idx = 2;
                for (int i = 0; i < sampleSize; i++)
                {
                    if (line[idx++] == '1')
                        sample[i] = true;
                    else
                        sample[i] = false;
                }

                this.letterData.Add(ch, sample);
                this.letters.Items.Add("" + ch);
            }

            f.Close();
        }
        MessageBox.Show(this, "File Loaded");

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失眠症患者 2025-01-04 05:07:16

删除以下行?

this.letterData.Clear();
this.letters.Items.Clear();

编辑:

或者更改为以下以便在字典中拥有唯一的键

this.letterData.Add(string.Format("{0}_{1}", openFileDialog1.FileName, ch), sample);
this.letters.Items.Add(ch.toString());

Remove the following lines?

this.letterData.Clear();
this.letters.Items.Clear();

EDIT:

Or change to te following in order to have unique keys in your dictionary

this.letterData.Add(string.Format("{0}_{1}", openFileDialog1.FileName, ch), sample);
this.letters.Items.Add(ch.toString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文