如何使用BinaryReader并将数据正确输入到文件中?

发布于 2024-12-11 04:42:48 字数 5124 浏览 1 评论 0原文

我正在做家庭作业,但我完全陷入困境!我想做的是使用已经定义的输入并使用 saveDataTo() 方法将其保存到文件中,并使用 readDataFrom() 方法读取输入。

我被困在第一部分。我不确定是否必须先初始化 Program.cs 文件中的数据?

我不知道,我被困住了。这是代码,希望能给我一些如何完成此任务的提示。

-- 编辑 --

我可以添加用于 saveDataTo() 和 readDataFrom() 方法的指令:

saveDataTo( ) 方法采用 BinaryWriter 参数。方法 将书籍对象的所有 5 个属性的值写入文件 与作者关联的流(关联是在 Program 类的 Main() 方法)。无需打开和关闭 该方法内的文件流和二进制编写器。

readDataFrom() 方法采用 BinaryReader 参数。这 方法从 Book 对象读取所有五个属性的值 与阅读器关联的文件流(关联是在 Program 类的 Main( ) 方法)。无需打开和 关闭此方法内的文件流和二进制读取器。

那么这给了我一个线索,我应该使用并分配要保存在文件中的属性?

-- 编辑 --

更新了那里的代码。我确实对保存到文件中的内容有疑问。我没有看到价格。这是为什么?

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);
        }
    }
}

问候。

帮助需要者。

I am working on my homework assignment and I am completely stuck! What I am trying to do is to use already defined input and save it to the file by using saveDataTo() method and read the input by using readDataFrom() method.

I am stuck on the first part. I am not sure if I have to initialize the data in Program.cs file first?

I don't know and I am stuck. Here is code and hope for some tips how I can accomplish this.

-- EDIT --

I can add instructions for purpose of both saveDataTo() and readDataFrom() methods:

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.

So that gives me a clue that I should use and assign the properties to be saved in the file there?

-- EDIT --

Updated the code there. I do have a problem with content that is being saved into the file. I am not being showed the price. Why is that?

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);
        }
    }
}

Regards.

HelpNeeder.

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

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

发布评论

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

评论(2

蒲公英的约定 2024-12-18 04:42:48

您问的是是否在 Program.cs 或 Book.cs 中定义值,对吗?好吧,在 Program.cs 中定义值就可以了,您只需要确保在写入数据之前给出所有值即可。

因此,由于该函数采用据称预先初始化的 BinaryWriter 参数,因此这应该可以工作:

public void saveDataTo(BinaryWriter w)
{
     w.Write(getAuthorName());
     //etc...
}

但是,请记住,您确实需要在调用保存数据之前在某处(任何地方)定义所有信息。

You're asking whether to define the values in Program.cs or Book.cs, right? Well, it is fine to define the values in Program.cs, you just need to make sure all the values are given before writing the data.

So, since the function takes a BinaryWriter parameter that is supposedly initialized beforehand, this should work:

public void saveDataTo(BinaryWriter w)
{
     w.Write(getAuthorName());
     //etc...
}

But, just remember that you do need to define all the info somewhere (anywhere) before calling save data.

游魂 2024-12-18 04:42:48

您将参数分配给 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。按照惯例,通常以大写(帕斯卡大小写)开始您的方法

编辑

您现在在读取时显示价格,因为您将其写为浮动字段(或双精度,看不到定义)并读取它作为整数。

从 r.ReadInt32() 更改;到 r.ReadDouble();或 r.ReadSingle()

You assign your parameters to 2 different objects, see:

Publication publication = new Publication(); 
Book book = new Book(); 

Both are individual instances residing in memory.

You either have to refer the publication to the book like:

Book book = new Book(); 
Publication publication = (Publication)book;

or just assign the values currently assigned to the publication directly to the book so:

publication.PublisherName = "PublisherName"; 

becomes

book.PublisherName = "PublisherName"; 

Apart from that, you're working in C#, not Java. By convention its normal to start your methods with a Capital (Pascal Case)

EDIT

Your now shown the price when reaidng since you write it as a floating field (or double, cant see the definition) and read it as an integer.

Change from r.ReadInt32(); to r.ReadDouble(); or r.ReadSingle()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文