文件流、流读取器和流写入器的问题
我的第一个问题的形式是,如果我以这种方式声明我的文件流等,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,它们已声明但未分配...因此,要么将它们设置为 null,要么一起执行所有操作。
Well, they're declared but unassigned... so, either set them to null or just do everything together.
您需要为顶部的变量分配一个值,即使它只是 null
You need to assign a value to your variables at the top, even if its just null
在关闭文件之前,请尝试刷新输出流和文件。
Before closing the files, try flushing the output streams and files.