Where should my using statements go? And where to place the try catch?
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开发者_如何学C);
}
}
}
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:
// Are you *sure* you want to use ASCII? UTF-8 might be a better bet...
using (TextWriter writer = File.CreateText("Shipping2.txt", Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
// Removed redundant ToString call, and elided local variable.
// Consider using a format string instead:
// writer.WriteLine("{0},{1}", shipment.Distance, shipment.Distance);
writer.WriteLine(shipment.Distance + "," + shipment.Distance);
}
// Removed empty statement (trailing semi-colon)
}
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?
Firstly, I dont understand the use for so many streams but you can use using in a number of places:
internal static void SaveFileAsTxt()
{
using(var streamer = new FileStream("Shipping2.txt", FileMode.Append, FileAccess.Write, FileShare.Write))
{
}
using(var f = File.Open("Shipping2.txt", FileMode.Create))
{
}
using(var writer = new StreamWriter("Shipping2.txt", true, Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
string write = (shipment.Distance + ","+ shipment.Distance).ToString();
writer.WriteLine(write);
};
}
}
You can skip two first FileStreams
and just use StreamWriter
, it will create a file for you :
// Single using
using (StreamWriter writer = new StreamWriter(
"Shipping2.txt",
true, // !!!
Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
string write = (shipment.Distance + "," + shipment.Distance)
.ToString();
writer.WriteLine(write);
}
}
MSDN:
public StreamWriter(
string path,
bool append,
Encoding encoding
)
append
Type: System.Boolean Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.
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:
If the file exists and append is false, the file is overwritten
Right now, the code you have is equivalent to this:
internal static void SaveFileAsTxt() {
using (FileStream streamer = new FileStream(
"Shipping2.txt", FileMode.Append, FileAccess.Write, FileShare.Write
);) {}
using(FileStream f = File.Open("Shipping2.txt", FileMode.Create)) {}
using(StreamWriter writer =
new StreamWriter("Shipping2.txt", true, Encoding.ASCII)) {
foreach (var shipment in _shipments) {
string write = (shipment.Distance + "," + shipment.Distance).ToString();
writer.WriteLine(write);
}
}
}
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.
// Single using
using (StreamWriter writer = new StreamWriter(
"Shipping2.txt",
true, // !!!
Encoding.ASCII))
{
foreach (var shipment in _shipments)
{
string write = (shipment.Distance + "," + shipment.Distance)
.ToString();
writer.WriteLine(write);
}
}
Just to be different, I'll use LINQ :-)
File.WriteAllLines(
"Shipping2.txt",
_shipments.Select(
p => string.Format(
"{0},{1}",
shipment.Distance,
shipment.Distance)
),
Encoding.ASCII);
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 containing shipment.Distance + "," + shipment.Distance
(formatted using string.Format
)
`
精彩评论