正在从二进制文件中读取列表
本文关键字:读取 列表 二进制文件 | 更新日期: 2023-09-27 18:00:28
我遇到的问题是,我可以准备好文件,但我的读取代码似乎只读取了2值列表中的1值。我不知道哪里出了问题。代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace test
{
public partial class Form1 : Form
{
[Serializable]
public class ore
{
public float Cost;
}
private List<ore> oreData = new List<ore>();
private ore b1 = null;
private ore b2 = null;
public Form1()
{
InitializeComponent();
b1 = new ore();
b2 = new ore();
oreData.Add(b1);
oreData.Add(b2);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 1st text box input is float
float tempFloat;
if (float.TryParse(textBox1.Text, out tempFloat))
{
oreData[0].Cost = tempFloat;
}
else
MessageBox.Show("uh oh");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
// 2nd text box input is float
float tempFloat;
if (float.TryParse(textBox1.Text, out tempFloat))
{
oreData[1].Cost = tempFloat;
}
else
MessageBox.Show("uh oh");
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("ore.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, oreData);
fs.Close();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
// 3rd text box
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
//4th text box
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("ore.dat", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
oreData = (List<ore>)bf.Deserialize(fs);
fs.Close();
if (oreData!=null)
{
if (oreData.Count > 0)
textBox3.Text = oreData[0].Cost.ToString();//update the 3rd text box
if (oreData.Count > 1)
textBox4.Text = oreData[1].Cost.ToString();//update the 4th text box
}
}
}
}
我发现了你的问题!(哦,我的眼睛疼!)
查看textbox1_changed
和textbox2_changed
方法中的if
语句。它们都是
if (float.TryParse(textBox1.Text, out tempFloat))
但第二个应该说
if (float.TryParse(textBox2.Text, out tempFloat))
请注意,textbox1
已更改为textbox2
。这应该能解决你的问题。
您需要定义一个List并将b1、b2项添加到其中,然后无论何时序列化或反序列化数据,都要将其反序列化到同一列表中。
注意:最好将TextBoxes和Buttons重命名为可读性更强的内容,如:而不是textBox2
=>FirstBookEpertonTextBox
、textBox2_TextChanged
=>firstBookEpertonTextBox_TextChanged
或OnFirstBookEpertonTextBox_TextChanged
、button1
=>saveBooksButton
、Form1
=>BooksMainForm
。。。。
更新:还要考虑只使用列表而不是b1和b2,比如:books[0]而不是b1,books[1]而不是b2,所以如果书籍数量增加,代码的可维护性就不会有问题。请注意,无论如何都要使用列表进行序列化和反序列化。
[Serializable]
public class ore
{
public float Cost;
}
private List<ore> books = new List<ore>(); // create a list at class level
public Form1()
{
InitializeComponent();
books.Add(new ore());
books.Add(new ore());
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 1st text box input is float
float tempFloat;
if (float.TryParse(textBox1.Text, out tempFloat))
{
books[0].Cost = tempFloat;
}
else
MessageBox.Show("uh oh");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
// 2nd text box input is float
float tempFloat;
if (float.TryParse(textBox2.Text, out tempFloat))
{
books[1].Cost = tempFloat;
}
else
MessageBox.Show("uh oh");
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("ore.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, books);
fs.Close();
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("ore.dat", FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
/*use the old list don't create new one
and also the new one you are creating has the same
name as the class level one which may makes conflicts to you.*/
books = (List<ore>)bf.Deserialize(fs);
fs.Close();
if (books!=null)
{
if (books.Count > 0)
{
//we don't need d1, d2 any more
//b1 = books[0];
textBox3.Text = books[0].Cost.ToString();
}
if (books.Count > 1)
{
//we don't need d1, d2 any more
//b2 = books[1];
textBox4.Text = books[1].Cost.ToString();
}
}
}