文件流、流读取器和流写入器的问题

发布于 2024-12-08 19:41:09 字数 1607 浏览 1 评论 0原文

我的第一个问题的形式是,如果我以这种方式声明我的文件流等,

filestream file;
streamreader file_in;
streamwriter file_out;

try
{
    file = new filestream("data.txt", FileMode.OpenOrCreate);
    file_in = new streamreader(file);
    file_out = new streamwriter(file);
}
catch(IOException exc)
{
    Console.WriteLine(exc.Message);
}

则会抛出一个错误,提示“使用未分配的局部变量”,我觉得这很奇怪,因为所有流都是在 try 块之外但在 main 中声明的,因此它们应该存在于 main 中。

我的另一个问题的形式是,如果我删除 try/catch 块并将流声明为一行(例如: FileStream file = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite) ;)我从文件中的读取确实有效,但是我无法写入文件。我的写入文件功能如下:

    public bool write_to_file(ref StreamWriter file_out)
    {
        if (this.is_empty == true)
        {
            Console.WriteLine("error, there is nothing to write.");
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            return false;
        }

        try
        {
            string temp = this.is_empty + "," + this.movie_title + "," + this.year_released + "," + this.publisher + "," +
                this.length + "," + this.acting_rating + "," + this.music_rating + "," + this.cinematography_rating + "," +
                this.plot_rating + "," + this.duration_rating + "," + this.total_rating;
            file_out.WriteLine(temp);
            return true;
        }
        catch (IOException exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            return false;
        }
    }

任何帮助将不胜感激,谢谢。

my first problem comes in the form of if i declare my filestream etc in this manner

filestream file;
streamreader file_in;
streamwriter file_out;

try
{
    file = new filestream("data.txt", FileMode.OpenOrCreate);
    file_in = new streamreader(file);
    file_out = new streamwriter(file);
}
catch(IOException exc)
{
    Console.WriteLine(exc.Message);
}

throws an error which says "use of unassigned local variable", which i find odd because all streams are declared outside of the try block but within the main so they should exist within the main.

my other problem comes in the form that if i remove the try/catch block and just declare the streams as one line (eg: FileStream file = new FileStream("data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);) my reading from file does work however i cannot write to file. my write to file function is as follows:

    public bool write_to_file(ref StreamWriter file_out)
    {
        if (this.is_empty == true)
        {
            Console.WriteLine("error, there is nothing to write.");
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            return false;
        }

        try
        {
            string temp = this.is_empty + "," + this.movie_title + "," + this.year_released + "," + this.publisher + "," +
                this.length + "," + this.acting_rating + "," + this.music_rating + "," + this.cinematography_rating + "," +
                this.plot_rating + "," + this.duration_rating + "," + this.total_rating;
            file_out.WriteLine(temp);
            return true;
        }
        catch (IOException exc)
        {
            Console.WriteLine(exc.Message);
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
            return false;
        }
    }

any help would be much appreciated, thanks.

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

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

发布评论

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

评论(3

后来的我们 2024-12-15 19:41:09

好吧,它们已声明但未分配...因此,要么将它们设置为 null,要么一起执行所有操作。

try
{
    using(var file = new FileStream("data.txt", FileMode.OpenOrCreate))
    using(var file_in = new StreamReader(file))
    using(var file_out = new StreamWriter(file))
    {
        // Do your thing
    }
}
catch
{
    throw;
}

Well, they're declared but unassigned... so, either set them to null or just do everything together.

try
{
    using(var file = new FileStream("data.txt", FileMode.OpenOrCreate))
    using(var file_in = new StreamReader(file))
    using(var file_out = new StreamWriter(file))
    {
        // Do your thing
    }
}
catch
{
    throw;
}
半城柳色半声笛 2024-12-15 19:41:09

您需要为顶部的变量分配一个值,即使它只是 null

FileStream   file     = null;
StreamReader file_in  = null;
StreamWriter file_out = null;

You need to assign a value to your variables at the top, even if its just null

FileStream   file     = null;
StreamReader file_in  = null;
StreamWriter file_out = null;
我喜欢麦丽素 2024-12-15 19:41:09

在关闭文件之前,请尝试刷新输出流和文件。

 file_out.Flush();
 file.Flush(); // may be redundant but won't hurt

Before closing the files, try flushing the output streams and files.

 file_out.Flush();
 file.Flush(); // may be redundant but won't hurt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文