C# 如何使用BinaryReader并正确地将数据输入文件?
我正在做作业,我完全被卡住了!我试图做的是使用已经定义的输入,并使用saveDataTo()方法将其保存到文件中,然后使用readDataFrom()方法读取输入 我被第一部分难住了。我不确定是否必须先初始化Program.cs文件中的数据 我不知道,我被卡住了。下面是代码,希望能为我提供一些技巧 --编辑-- 我可以为saveDataTo()和readDataFrom()方法添加说明: saveDataTo()方法采用BinaryWriter参数。方法 将书本对象的所有5个属性的值写入文件 与写入程序关联的流(关联在 程序类的Main()方法)。不需要打开和关闭 此方法中的文件流和二进制编写器 readDataFrom()方法采用BinaryReader参数。这个 方法从中读取Book对象的所有五个属性的值 与读取器关联的文件流(关联在 程序类的Main()方法)。没有必要打开和关闭 关闭此方法中的文件流和二进制读取器 所以这给了我一个线索,我应该使用和分配属性保存在那里的文件 --编辑-- 更新了那里的代码。我确实对保存到文件中的内容有问题。我没有被告知价格。为什么呢C# 如何使用BinaryReader并正确地将数据输入文件?,c#,methods,binaryreader,binarywriter,C#,Methods,Binaryreader,Binarywriter,我正在做作业,我完全被卡住了!我试图做的是使用已经定义的输入,并使用saveDataTo()方法将其保存到文件中,然后使用readDataFrom()方法读取输入 我被第一部分难住了。我不确定是否必须先初始化Program.cs文件中的数据 我不知道,我被卡住了。下面是代码,希望能为我提供一些技巧 --编辑-- 我可以为saveDataTo()和readDataFrom()方法添加说明: saveDataTo()方法采用BinaryWriter参数。方法 将书本对象的所有5个属性的值写入文件 与
ff.APublisherNameTitle FirstNameLastName
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Program
{
private const string FILE_NAME = "lab07.dat";
static void Main(string[] args)
{
//char ask;
/*
do
{
Console.Write("Enter Book Title: ");
publication.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ");
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Publisher's Name: ");
publication.PublisherName = Console.ReadLine();
Console.Write("Enter Book Price: $");
publication.Price = float.Parse(Console.ReadLine());
Console.Write("Would like to enter another book? [Y or N] ");
ask = char.Parse(Console.ReadLine().ToUpper());
}
while (ask == char.Parse("Y"));
*/
Book book = new Book();
book.Price = 10.9F;
book.Title = "Title";
book.PublisherName = "PublisherName";
book.AuthorFirstName = "FirstName";
book.AuthorLastName = "LastName";
FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
BinaryWriter write = new BinaryWriter(fileStream);
book.saveDataTo(write);
write.Close();
fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(fileStream);
book.readDataFrom(read);
read.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Publication
{
private float price;
private string publisherName, title;
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string PublisherName
{
get
{
return publisherName;
}
set
{
publisherName = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public void display()
{
Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Book : Publication
{
private string authorFirstName, authorLastName;
public string AuthorFirstName
{
get
{
return authorFirstName;
}
set
{
authorFirstName = value;
}
}
public string AuthorLastName
{
get
{
return authorLastName;
}
set
{
authorLastName = value;
}
}
public new void display()
{
}
public string getAuthorName()
{
return authorFirstName + " " + authorLastName;
}
public void readDataFrom(BinaryReader r)
{
Price = r.ReadInt32();
PublisherName = r.ReadString();
Title = r.ReadString();
authorFirstName = r.ReadString();
authorLastName = r.ReadString();
}
public void saveDataTo(BinaryWriter w)
{
w.Write(base.Price);
w.Write(base.PublisherName);
w.Write(base.Title);
w.Write(AuthorFirstName);
w.Write(AuthorLastName);
}
}
}
Publication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Program
{
private const string FILE_NAME = "lab07.dat";
static void Main(string[] args)
{
//char ask;
/*
do
{
Console.Write("Enter Book Title: ");
publication.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ");
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Publisher's Name: ");
publication.PublisherName = Console.ReadLine();
Console.Write("Enter Book Price: $");
publication.Price = float.Parse(Console.ReadLine());
Console.Write("Would like to enter another book? [Y or N] ");
ask = char.Parse(Console.ReadLine().ToUpper());
}
while (ask == char.Parse("Y"));
*/
Book book = new Book();
book.Price = 10.9F;
book.Title = "Title";
book.PublisherName = "PublisherName";
book.AuthorFirstName = "FirstName";
book.AuthorLastName = "LastName";
FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
BinaryWriter write = new BinaryWriter(fileStream);
book.saveDataTo(write);
write.Close();
fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(fileStream);
book.readDataFrom(read);
read.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Publication
{
private float price;
private string publisherName, title;
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string PublisherName
{
get
{
return publisherName;
}
set
{
publisherName = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public void display()
{
Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Book : Publication
{
private string authorFirstName, authorLastName;
public string AuthorFirstName
{
get
{
return authorFirstName;
}
set
{
authorFirstName = value;
}
}
public string AuthorLastName
{
get
{
return authorLastName;
}
set
{
authorLastName = value;
}
}
public new void display()
{
}
public string getAuthorName()
{
return authorFirstName + " " + authorLastName;
}
public void readDataFrom(BinaryReader r)
{
Price = r.ReadInt32();
PublisherName = r.ReadString();
Title = r.ReadString();
authorFirstName = r.ReadString();
authorLastName = r.ReadString();
}
public void saveDataTo(BinaryWriter w)
{
w.Write(base.Price);
w.Write(base.PublisherName);
w.Write(base.Title);
w.Write(AuthorFirstName);
w.Write(AuthorLastName);
}
}
}
Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Program
{
private const string FILE_NAME = "lab07.dat";
static void Main(string[] args)
{
//char ask;
/*
do
{
Console.Write("Enter Book Title: ");
publication.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ");
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Publisher's Name: ");
publication.PublisherName = Console.ReadLine();
Console.Write("Enter Book Price: $");
publication.Price = float.Parse(Console.ReadLine());
Console.Write("Would like to enter another book? [Y or N] ");
ask = char.Parse(Console.ReadLine().ToUpper());
}
while (ask == char.Parse("Y"));
*/
Book book = new Book();
book.Price = 10.9F;
book.Title = "Title";
book.PublisherName = "PublisherName";
book.AuthorFirstName = "FirstName";
book.AuthorLastName = "LastName";
FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
BinaryWriter write = new BinaryWriter(fileStream);
book.saveDataTo(write);
write.Close();
fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(fileStream);
book.readDataFrom(read);
read.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Publication
{
private float price;
private string publisherName, title;
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string PublisherName
{
get
{
return publisherName;
}
set
{
publisherName = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public void display()
{
Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Book : Publication
{
private string authorFirstName, authorLastName;
public string AuthorFirstName
{
get
{
return authorFirstName;
}
set
{
authorFirstName = value;
}
}
public string AuthorLastName
{
get
{
return authorLastName;
}
set
{
authorLastName = value;
}
}
public new void display()
{
}
public string getAuthorName()
{
return authorFirstName + " " + authorLastName;
}
public void readDataFrom(BinaryReader r)
{
Price = r.ReadInt32();
PublisherName = r.ReadString();
Title = r.ReadString();
authorFirstName = r.ReadString();
authorLastName = r.ReadString();
}
public void saveDataTo(BinaryWriter w)
{
w.Write(base.Price);
w.Write(base.PublisherName);
w.Write(base.Title);
w.Write(AuthorFirstName);
w.Write(AuthorLastName);
}
}
}
问候
HelpNeeder.您是在问是在Program.cs还是Book.cs中定义值,对吗?好的,在Program.cs中定义值是可以的,您只需要确保在写入数据之前给出了所有的值 因此,由于该函数采用假定已预先初始化的BinaryWriter参数,因此应该可以:
public void saveDataTo(BinaryWriter w)
{
w.Write(getAuthorName());
//etc...
}
但是,请记住,在调用“保存数据”之前,您确实需要在某处(任何地方)定义所有信息。如果要将参数分配给两个不同的对象,请参阅:
Publication publication = new Publication();
Book book = new Book();
两者都是驻留在内存中的单个实例
您必须将出版物引用到以下书籍:
Book book = new Book();
Publication publication = (Publication)book;
或者直接将当前分配给出版物的值分配给书籍,以便:
publication.PublisherName = "PublisherName";
变成
book.PublisherName = "PublisherName";
除此之外,您使用的是C#,而不是Java。按照惯例,用大写字母开始你的方法是很正常的(帕斯卡)
编辑
您现在在重新读取时显示了价格,因为您将其作为浮动字段(或双精度字段,看不到定义)写入,并将其作为整数读取
从r.ReadInt32()更改;到r.ReadDouble();或者r.ReadSingle()好的,我想应该是这样的。我必须在Main()中添加一些东西,以便BinaryReader()知道文件流吗?看看Program.cs中的第47行,不需要它吗?第47行是哪个?您在其中设置了新的BinaryWriter?我在它前面添加了注释行抱歉,不是第47行。它在program.cs部分。您确实需要声明一个新的BinaryWriter,这样saveDateTo()函数就可以实际将数据写入某些东西。噢!好啊我知道你在说什么。因为我继承了参数,所以我应该使用book对象而不是Publishing。太好了,它可以编译。如果我有更多的问题,我会发布更多的问题。我的教授坚持我们用camel case来表示方法和类的大小写。你的教授真的教了你一个糟糕的做法。。。我并不知道什么是最好的,但我坚信,让事情符合行业标准是很重要的,除此之外,作为开发人员,您应该决定如何编写代码,并更新我的答案以解决您的第二个问题。现在关于你的老师(主题外)。如果你认为你可以改进别人的工作,即使是乔恩·斯基特本人,那就试着去做吧!好的,如果它是Float,那么您所做的唯一更改是在第一行的readDataFrom中,从“Price=r.ReadIn32();”更改为“Price=r.ReadSingle();”