问题
I have a simple program where I write 6 of 7 numbers to a text file. Logically everything seems to be fine.
However the numbers are not written to the file as expected.
Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
//creating the lotto file
FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bs = new BufferedStream(fs);
Console.WriteLine("File created");
fs.Close();
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
//Console.Write(random.Next(1, 49));
sw.Write(random.Next(1, 49) + " " );
}
sw.WriteLine();
}
sw.Close();
The file was created, however no numbers were written to the file...advice perhaps as to why?
回答1:
Note that your code is not optimized and has a lot of unnecessary streams and buffers being created but the answer by @Michael outlines the right code to use in it's place. My answer will just highlight why your code wasn't working in the intended way.
The answer to your question is actually very simple.
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");
You have forgotten to add the /
in the string to ../..
. If fileLotto
is assumed to have the value example
then the FileStream
will create the file example.txt
but the StreamWriter
will access ..example.txt
for writing and that too in a different folder.
Use variables to define values that have to be repeated used. Remember the DRY principle.
Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
string fileName = "../../" + fileLotto + ".txt";
//creating the lotto file
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
Console.WriteLine("File created");
fs.Close();
StreamWriter sw = new StreamWriter(fileName);
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
//Console.Write(random.Next(1, 49));
sw.Write(random.Next(1, 49) + " " );
}
Console.WriteLine();
}
sw.Close();
Again I say please use @Michael's code. This is just to highlight the primary issue with your code.
回答2:
What are you trying to do? Whay you declare so many streams for nothing? Just use:
using(StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt")){
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++)
{
//Console.Write(random.Next(1, 49));
sw.Write(random.Next(1, 49) + " " );
}
Console.WriteLine();
}
}
http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx
回答3:
Well I have to admit that this is not a fancy code. But for why this is not working is this
In this line
FileStream fs = new FileStream("../../" + fileLotto + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
You are opening file in "../../"
folder which is two up folder of executable file.
But in this line
StreamWriter sw = new StreamWriter("../.." + fileLotto + ".txt");
Same parameter is "../.."
which causes another file to be opened parent folder of executable with ".."
in the beginnig of file name. You have add an extra '/'
at the end of StreamWriter parameter to ensure you are writing the first file you created using FileStream.
回答4:
Let's simplify this:
Random random = new Random();
Console.WriteLine("Please enter the name of the numbers file");
string fileLotto = Console.ReadLine();
StringBuilder text = new StringBuilder();
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
text.Append(random.Next(1, 49) + " " );
}
Console.WriteLine();
}
File.WriteAllText(string.Format("../../{0}.txt", fileLotto), text.ToString());
This code is also safer. You're not opening a bunch of streams (that you aren't closing BTW) that are unnecessary. Rather you are getting all of the text together and writing it all at once.
来源:https://stackoverflow.com/questions/14069642/writing-data-to-a-text-file