我的 using 语句应该放在哪里?以及在哪里放置 try catch?
我刚刚开始使用 filestream,虽然我让代码工作了,但我真的很想让它变得漂亮:)我不知道在哪里放置 using 语句,这样我就可以跳过stream.Close(),以及如何使用 try catch 最后。这是我的代码,不是最漂亮的东西,但它有效。双文件流用于清除文件。
编辑:很抱歉发布了那个非常糟糕的代码片段脸红:P我已经发布了我的第二次尝试:)
internal static void SaveFileAsTxt()
{
FileStream streamer = new FileStream("Shipping2.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
streamer.Close();
FileStream f = File.Open("Shipping2.txt", FileMode.Create);
f.Close();
StreamWriter writer = new StreamWriter("Shipping2.txt", true, Encoding.ASCII);
foreach (var shipment in _shipments)
{
string write = (shipment.Distance + ","+ shipment.Distance).ToString();
writer.WriteLine(write);
};
writer.Close();
}
//--------new code--------
internal static void SaveFileAsTxt()
{
if (File.Exists("Shipping2.txt"))
{
File.Delete("Shipping2.txt");
}
using (StreamWriter writer = new StreamWriter("Shipping2.txt", true, Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
string write = (shipment.Duration + ","+ shipment.Distance).ToString();
writer.WriteLine(write);
}
}
}
I've just started using filestream, and although I made the code work,- I would really like to make it pretty as well :) I have no idea where to place the using statements so I can skip the stream.Close(), and how to use try catch finally. here is my code, not the prettiest thing, but it works. The double filestream is used to clear the file.
Edit: sorry for posting that code snippet blush that was pretty bad :P I've posted my second try :)
internal static void SaveFileAsTxt()
{
FileStream streamer = new FileStream("Shipping2.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
streamer.Close();
FileStream f = File.Open("Shipping2.txt", FileMode.Create);
f.Close();
StreamWriter writer = new StreamWriter("Shipping2.txt", true, Encoding.ASCII);
foreach (var shipment in _shipments)
{
string write = (shipment.Distance + ","+ shipment.Distance).ToString();
writer.WriteLine(write);
};
writer.Close();
}
//--------new code--------
internal static void SaveFileAsTxt()
{
if (File.Exists("Shipping2.txt"))
{
File.Delete("Shipping2.txt");
}
using (StreamWriter writer = new StreamWriter("Shipping2.txt", true, Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
string write = (shipment.Duration + ","+ shipment.Distance).ToString();
writer.WriteLine(write);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不需要多次打开该文件 - 实际上您此时已打开它三次次。这应该没问题;
File.CreateText
如果文件已经存在,则将截断该文件,否则创建它:现在,您说您想使用 try/catch/finally - 但为什么呢?如果无法写入文件,您是否一定要在此方法中“处理”异常,而不是让它冒泡给调用者?
You don't need to open the file more than once - and you're actually opening it three times at the moment. This should be fine;
File.CreateText
will truncate the file if it already exists, and create it otherwise:Now, you say you want to use try/catch/finally - but why? If you fail to write to the file, do you definitely want to "handle" the exception in this method, rather than letting it bubble up to the caller?
首先,我不明白这么多流的用途,但您可以在很多地方使用 using :
Firstly, I dont understand the use for so many streams but you can use using in a number of places:
您可以跳过前两个
FileStreams
并仅使用StreamWriter
,它会为您创建一个文件:MSDN:
附加
编辑:关于更新的问题(第二部分)
您不需要手动删除文件,只需在
StreamWriter
的构造函数中指定append = false,它将覆盖文件:You can skip two first
FileStreams
and just useStreamWriter
, it will create a file for you :MSDN:
append
EDIT: Regarding an updated question (second part)
You do not need to delete file manually, just specify append = false in constructor of
StreamWriter
and it will overwrite a file:现在,您拥有的代码相当于:
但我不知道您为什么打开第一个文件。
关于第二个,我相信您尝试使用此方法创建文件,
但还有其他方法可以做到这一点。
Right now, the code you have is equivalent to this:
But I don't know why you opened the first file.
In respect to the second, I believe you tried to create the file using this method,
but there are other ways to do that as well.
为了与众不同,我将使用 LINQ :-)
此版本需要 .NET 4.0。
File.WriteAllLines
将创建文件,写入所有行并关闭文件。对于_shipments< 的每个元素,
_shipments.Select(p => string.Format("{0},{1}",shipment.Distance,shipment.Distance))
将返回/code>,包含shipment.Distance + "," +shipment.Distance
的字符串(使用string.Format
格式化)`
Just to be different, I'll use LINQ :-)
This version requires .NET 4.0.
File.WriteAllLines
will create the file, write all the lines and close the file._shipments.Select(p => string.Format("{0},{1}", shipment.Distance, shipment.Distance))
will return, for each element of_shipments
, a string containingshipment.Distance + "," + shipment.Distance
(formatted usingstring.Format
)`