CodeGo.net>如何使用BinaryReader和正确地输入数据到文件?
作者:互联网
我正在做作业,我完全陷入困境!我正在尝试使用已定义的输入,并通过使用saveDataTo()方法将其保存到文件中,并通过使用readDataFrom()方法来读取输入.
我停留在第一部分.我不确定是否必须先初始化Program.cs文件中的数据?
我不知道,我被卡住了.这是代码,并希望获得一些技巧来实现这一目标.
-编辑-
我可以同时为saveDataTo()和readDataFrom()方法添加指令:
The saveDataTo( ) method takes a parameter of BinaryWriter. The method
writes the values of all 5 properties of an book object to a file
stream associated with the writer (the association is done in the
Main( ) method of Program class). There is no need to open and close
the file stream and binary writer inside this method.The readDataFrom( ) method takes a parameter of BinaryReader. The
method reads the values of all five properties of the Book object from
a file stream associated with the reader (the association is done in
the Main( ) method of Program class). There is no need to open and
close the file stream and binary reader inside this method.
这样就给了我一个线索,我应该使用它并分配要保存在文件中的属性?
-编辑-
在此更新了代码.我确实对保存到文件中的内容有疑问.没有显示价格.这是为什么?
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();
}
}
}
Publication.cs
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);
}
}
}
Book.cs
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.
解决方法:
您将参数分配给2个不同的对象,请参见:
Publication publication = new Publication();
Book book = new Book();
两者都是驻留在内存中的单个实例.
您要么必须像下面那样参考出版物到书:
Book book = new Book();
Publication publication = (Publication)book;
或仅将当前分配给出版物的值直接分配给该书,这样:
publication.PublisherName = "PublisherName";
变成
book.PublisherName = "PublisherName";
除此之外,您使用的是C#,而不是Java.按照惯例,以大写字母(Pascal Case)开始您的方法是正常的
编辑
您现在在重新获取价格时显示了价格,因为您将其写为一个浮动字段(或加倍,看不到定义)并将其读为整数.
从r.ReadInt32()更改;到r.ReadDouble();或r.ReadSingle()
标签:binaryreader,binarywriter,c,methods 来源: https://codeday.me/bug/20191202/2086633.html