程序关闭导致 StreamWriter 混乱
我正在开发一个小型 ATM 程序,但我遇到了 StreamWritter 问题。 加载时,我的程序必须使用 StreamReader 读取位于我的 bin/debug 中的 4 个 .txt 文件。然后,系统会要求用户从 .txt 文件中的银行帐户存款或取款。对于 StreamReader 在程序加载时加载所有银行帐户以及 StreamWriter 在我添加/删除资金时将更改写入 .txt 文件来说,一切都工作正常。 我的问题是当我关闭程序时,文件的加载工作正常,但我无法再在文件中写入。我的 StreamWriter 直接跳转到 Catch 部分并且无法实例化。如果第一次使用时效果很好,那怎么可能呢? 这是 StreamReader 代码:
public bool ReadSavingAccount()
{
string strLine;
string[] strArray;
char[] charArray = new char[] { ',' };
FileStream aFile;
StreamReader sr;
try
{
aFile = new FileStream("mySavingAccount.txt", FileMode.Open);
sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
Savings monSave = new Savings(strArray[0], Convert.ToDouble(strArray[1]));
mySavingAccount.Add(monSave);
strLine = sr.ReadLine();
}
sr.Close();
aFile.Close();
}
catch
{
return false;
}
return true;
}
和 StreamWrite 代码:
public bool WriteSavingAccount()
{
FileStream aFile;
StreamWriter sw;
string myString;
try
{
aFile = new FileStream("mySavingAccount.txt", FileMode.Create);
sw = new StreamWriter(aFile);
}
catch
{
return false;
}
foreach (Savings mySave in mySavingAccount)
{
myString = mySave.AccountNumber + "," + mySave.AccountBalance;
sw.WriteLine(myString);
}
sw.Close();
return true;
}
知道问题可能是什么吗? 提前致谢,如果您需要代码的任何其他部分,请告诉我。
Im working on a little ATM program and I'm stuck on a StreamWritter problem.
On load, my program must use a StreamReader to read in 4 .txt files all located in my bin/debug. Then the user is asked to either Deposit or Withdraw money from the bank accounts located in the .txt files. Everything works fine for the StreamReader which loads all the bank accounts on program loading and for the StreamWriter to write the changes in the .txt files when I add/remove money.
My problem is when I close the program, the loading of the files works just fine but I can't write in the files anymore. My StreamWriter jumps straight to the Catch part and cannot be instantiated. How is that possible if it worked just fine on first use.
Heres the StreamReader Code :
public bool ReadSavingAccount()
{
string strLine;
string[] strArray;
char[] charArray = new char[] { ',' };
FileStream aFile;
StreamReader sr;
try
{
aFile = new FileStream("mySavingAccount.txt", FileMode.Open);
sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while (strLine != null)
{
strArray = strLine.Split(charArray);
Savings monSave = new Savings(strArray[0], Convert.ToDouble(strArray[1]));
mySavingAccount.Add(monSave);
strLine = sr.ReadLine();
}
sr.Close();
aFile.Close();
}
catch
{
return false;
}
return true;
}
And the StreamWrite Code:
public bool WriteSavingAccount()
{
FileStream aFile;
StreamWriter sw;
string myString;
try
{
aFile = new FileStream("mySavingAccount.txt", FileMode.Create);
sw = new StreamWriter(aFile);
}
catch
{
return false;
}
foreach (Savings mySave in mySavingAccount)
{
myString = mySave.AccountNumber + "," + mySave.AccountBalance;
sw.WriteLine(myString);
}
sw.Close();
return true;
}
Any idea what the problem could be ?
Thanks in advance and let me know if you need any other parts of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将第二个布尔参数添加到 StreamWriter 调用中。这是追加参数。 True 表示附加到文件,False 表示覆盖文件。
Try adding a second parameter of boolean to your StreamWriter call. This is the append parameter. True to append to the file, false to overwrite it.
在
using
语句中创建您的StreamWriter
以确保其正确关闭。Create your
StreamWriter
in ausing
statement to ensure it is closed properly.