1

ストリーム ライターを作成し、コンソールからの入力をファイルに書き込んでから、コンテンツを読み込もうとしています。唯一の問題は、2 つの異なる方法で実行したいことです。ここから書きます: ここaddbook()から読みます: list_books(). しかし、ストリーム リーダーでは、変数やそのファイルにアクセスして再度使用することはできないため、そのファイルから読み取ることはできません。

//So I am trying to write from the static void addbook method and then read from the static void list_books method

//I want to Write from one static void and then I want  to read them from a different method.

static void addbook()
{
    //Here is where I get my strings that I will write

    Console.Write("Book Title:");
    string title = Console.ReadLine();

    Console.Write("ISBN#:");
    string isbn = Console.ReadLine();

    Console.Write("Author:");
    string author = Console.ReadLine();

    Console.Write("Publish Date:");
    string publish_date = Console.ReadLine();

    //Here Is where I create the Stream Reader and Writer
    //And where I write to the file
    var fs = File.Open("Librarybooks.txt", FileMode.OpenOrCreate,FileAccess.ReadWrite);
    var sw = new StreamWriter(fs);
    var sr = new StreamReader(fs);

    sw.WriteLine(title.ToCharArray());
    sw.WriteLine(isbn.ToCharArray());
    sw.WriteLine(author.ToCharArray());
    sw.WriteLine(publish_date.ToCharArray());

    Console.WriteLine("Book added Successfully!!");
}

static void list_books()
{
    //Here is where I want to read from so I can just call the list_books method.
    //But I can't access the StreamReader or StreamWriter
    //I just want to be able to read from the file.
}
4

2 に答える 2

2

MSDN は、両方のことを行う方法の例を提供しています

例からわかるように、文字列を文字配列に変換して書き込む必要はありません。

usingまた、キーワードを使用した両方の例にも気付くでしょう。この質問 (およびおそらく他の多くの質問) で、なぜその構成を使用したいのかについての説明があります

既にこのクラスを気に入っているようFileです。テキスト ファイルを操作するために特別に設計された他のメソッドを利用できます。ファイルへの書き込み/読み取りを行単位で行うか、内容全体で行うかを決定できます。クラスの使用方法の例を次に示しFileます。


プログラミングを始めたばかりのように見えるコードに基づいて、コードをCodeReviewに配置することを検討してください。そうすれば、より良いコードの書き方に関する役立つヒントを得ることができます。

于 2013-01-29T02:59:36.907 に答える
1
private const string FILE_PATH = "Librarybooks.txt";

static void addbook()
{
    //Here is where I get my strings that I will write

    Console.Write("Book Title:");
    string title = Console.ReadLine();

    Console.Write("ISBN#:");
    string isbn = Console.ReadLine();

    Console.Write("Author:");
    string author = Console.ReadLine();

    Console.Write("Publish Date:");
    string publish_date = Console.ReadLine();

    //Here Is where I create the Stream Reader and Writer
    //And where I write to the file
    using (var fs = File.Open(FILE_PATH, FileMode.OpenOrCreate,FileAccess.ReadWrite))
    {
        using (var sw = new StreamWriter(fs))
        {
            using (var sr = new StreamReader(fs))
            {
                sw.WriteLine(title.ToCharArray());
                sw.WriteLine(isbn.ToCharArray());
                sw.WriteLine(author.ToCharArray());
                sw.WriteLine(publish_date.ToCharArray());

                Console.WriteLine("Book added Successfully!!");
             }
         }
     }
}

static void list_books()
{
    using (StreamReader sr = new StreamReader(FILE_PATH))
    {
       string fileContent = sr.ReadToEnd();
    }
}
于 2013-01-29T02:49:26.320 に答える