我的第一個問題就在,如果我聲明我FILESTREAM等以這種方式問題FILESTREAM,StreamReader的和StreamWriter
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;
}
}
任何幫助將不勝感激,謝謝。
任何你把'FileStream'改成'filestream'的原因? –
-1,您發佈的代碼(第一部分)是否__not__給出您聲稱的錯誤。 –