尝试从二进制文件将列表 <> 项添加到列表框 1 时出现流异常结束

End of Stream Exception when trying to add list<> items to listbox1 from binary file

我的项目将 phone 个数字存储到一个列表<>中并将它们保存到一个 .bin 文件中:

private void savebutton_Click(object sender, EventArgs e)
    {
        SaveFileDialog sv = new SaveFileDialog();
        sv.Filter = "Binary files (*.bin)|*.bin|All files(*.*)|*.*";
        sv.Title = "Save File";
        sv.FilterIndex = 2;
        sv.RestoreDirectory = true;
        sv.InitialDirectory = Path.GetFullPath(@"F:\Computer Technology Skills\Programming 35\Module 1\ICA10\ICA10\bin\Debug");
        if (sv.ShowDialog() == DialogResult.OK)
        {
            try
            {
                FileStream fs = new FileStream(sv.FileName, FileMode.Create, FileAccess.Write);
                BinaryWriter file = new BinaryWriter(fs);
                //         System.IO.StreamWriter file = new System.IO.StreamWriter(sv.FileName.ToString());
                var message = string.Join(Environment.NewLine, PhoneNum);
                file.Write(message);
                file.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); 
            }

        }
    }

然后按加载按钮应该会删除列表框 1 中当前的项目,以将它们替换为每行 bin 文件中的任何项目。

private void loadbutton_Click(object sender, EventArgs e)
    {
        OpenFileDialog od = new OpenFileDialog();
        string databyte = null;
        long iNumInts = 0;

        od.RestoreDirectory = true;
        od.InitialDirectory = Path.GetFullPath(@"F:\Computer Technology Skills\Programming 35\Module 1\ICA10\ICA10\bin\Debug");
        if (od.ShowDialog() == DialogResult.OK)
        {
            try
            {
                FileStream fs = new FileStream(od.FileName, FileMode.Open);
                listBox1.Items.Clear();
                using (BinaryReader reader = new BinaryReader(fs))
                {

                    iNumInts = fs.Length / sizeof(int);
                    for (int i = 0; i < iNumInts; i++)
                    {
                        databyte = reader.ReadString(); //Endofstreamexception
                        listBox1.Items.Add(databyte);

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

问题是,当我加载我保存的文件时,项目都卡在 listbox1 的第一个索引上,我收到一个 endofstream 异常。

我在这里有点困惑,因为我的笔记没有多大帮助,我发现的其他堆栈溢出问题是使用整数或数组的示例。我从 streamwriter 切换到 binarywriter,这帮了很多忙,但我们将不胜感激!

查看您的保存代码,您似乎将 phone 数字写成单个字符串,并在它们之间使用换行符。在读取代码中,您尝试使用 ReadString 方法将它们作为单独的字符串读取。

一串出等于一串入。换句话说,如果你把它们写成一个串,然后把它们都读成一个串。然后你可以使用 string.split 并获得个人号码。

为此我建议使用 StreamReader

using (StreamReader reader = new StreamReader(od.Filename))
{
    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        listbox1.Items.Add(line);
    }
}

您也可以使用 File.ReadLines,它会为您完成这一切(如果您使用的是 .NET 4 或更高版本)。