本文介绍了如何从一个类中的文本文件中读取并将其保存到另一个类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是C#的新手。我要做的是读取文本文件并在一个类中创建Book类的对象,并将所有book类对象保存在另一个类的Queue数据结构中。我不确定我是否做得对。谁能帮帮我吗。拜托,谢谢。



我尝试过:



Class从文本文件中读取

Hello there I am new at C#. what I have to do is to read text file and creat an object of Book class in one class and save all the book class objects in the Queue data structure in another class. I am not sure If I did right. Can anyone please help me. Please and thank you.

What I have tried:

Class to read from the text file

public Queue<book> readBookData()
        {
               Queue<book> books = new Queue<book>();
               StreamReader sr = new StreamReader("Books.txt");
          
            string line = "";
            while((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                Book bk = new Book();
                bk.Author = words[0];
                bk.Book_Name = words[1];
                bk.Publisher = words[2];
                bk.Year = int.Parse(words[3]);
                bk.Category = words[4];

                books.Enqueue(bk);
            }
            return books;
        }

//-----------------------------------------------------------------------
// The data Structure class

        static Queue<book> books = new Queue<book>();
        public Queue<book> readBooks()
        {
            return books;
        }

        public void addBook(Book b)
        {
            books.Enqueue(b);
        }

        public Book removeBook()
        {
            return books.Dequeue();
        }

推荐答案

IList<Book> bookList = new List<Book>() { 
                new Book(){ ID=1, Author="Bill Gates"},
                new Book(){ ID=2, Author="Steve Jobs"},
                new Book(){ ID=3, Author="Ronald McDonald"},
                new Book(){ ID=4, Author="John Doe"}
            };

列表<>比队列更通用,你可以添加,插入和删除等。

另见这里的例子: []

A List<> is more versatile than a Queue, you can Add, Insert and Delete etc.
Also see examples here: https://www.dotnetperls.com/initialize-list[^]


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonFill_Click(object sender, EventArgs e)
        {
            BooksHelper.Books = readBookData();
            Debug.Print("Total books = " + BooksHelper.Books.Count);
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            AnotherBookClass.WriteAllBooks("books2.txt");
        }

        public Queue<Book> readBookData()
        {
            Queue<Book> books = new Queue<Book>();
            StreamReader sr = new StreamReader("Books.txt");
            string line = string.Empty;

            while ((line = sr.ReadLine()) != null)
            {
                string[] words = line.Split(',');
                Book bk = new Book();
                bk.Author = words[0];
                bk.Book_Name = words[1];
                bk.Publisher = words[2];
                bk.Year = int.Parse(words[3]);
                bk.Category = words[4];
                books.Enqueue(bk);
            }

            return books;
        }
    }  // Form1 class ends here.

    public class Book
    {
        public string Author { get; set; }
        public string Book_Name { get; set; }
        public string Publisher { get; set; }
        public int Year { get; set; }
        public string Category { get; set; }

        override public string ToString()
        {
            return Author + "," + Book_Name + "," + Publisher + "," + Year + "," + Category + Environment.NewLine;
        }
    }

    public static class BooksHelper
    {
        public static Queue<Book> Books = new Queue<Book>();
    }

    public static class AnotherBookClass
    {
        public static void WriteAllBooks(string fileName)
        {
            foreach (Book item in BooksHelper.Books)
            {
                File.AppendAllText(fileName, item.ToString());
            }
        }
    }
}


这篇关于如何从一个类中的文本文件中读取并将其保存到另一个类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 01:45