【问题标题】:How to write to a text file in one method and then read from another method?如何以一种方法写入文本文件,然后从另一种方法读取?
【发布时间】:2013-01-29 02:39:41
【问题描述】:

我正在尝试制作一个 Stream Writer,然后将一些输入从控制台写入文件,然后读出内容。唯一的问题是我想用两种不同的方法来做。从这里写: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.
}

【问题讨论】:

  • 您必须在 list_books 方法的范围内声明您的 StreamReader 才能使用它。

标签: c# .net


【解决方案1】:

MSDN 提供了如何做这两件事的示例

从示例中可以看出,您不需要将字符串转换为字符数组来编写它们。

您还会注意到这两个示例都使用了using 关键字。在这个问题(可能还有很多其他问题)中解释了为什么要使用该构造:When should I use “using” blocks in C#?

看起来您已经喜欢File 类,您可以利用其中专门为处理文本文件而设计的其他方法。您可以决定是否要根据行或整个内容写入/读取文件。这里有如何使用File类的例子:


根据您似乎刚刚开始编程的代码,请考虑将您的代码放在CodeReview 上。这样,您可以获得有关如何编写更好代码的有用提示。

【讨论】:

    【解决方案2】:
    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();
        }
    }
    

    【讨论】:

    • 所以我这样做了,然后它给出了这个错误:“进程无法访问文件'C:\Users\Kyson Gardner\documents\visual studio 2010\Projects\Library\Library\bin\Debug \Librarybooks.txt',因为它正被另一个进程使用。”我该如何解决这个问题?
    • 你没有关闭文件流,或者我没有在代码中。我更新了答案。请尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2016-06-27
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多