【发布时间】:2015-01-22 21:08:02
【问题描述】:
我想直接写入文件系统,而不是每一行都写入一个字符串,因为那样会更耗时。
我是这样尝试的:
static void Main(string[] args)
{
string path = @"C:\BankNumber";
var bans = BankAcoutNumbers.BANS;
const int MAX_FILES = 80;
const int BANS_PER_FILE = 8181 / 80;
int bansCounter = 0;
var part = new List<int>();
var maxNumberOfFiles = 10;
Stopwatch timer = new Stopwatch();
var fileCounter = 0;
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
try
{
while (fileCounter <= maxNumberOfFiles)
{
timer.Start();
Console.WriteLine("Start writing the bank numbers to file system");
// Console.WriteLine(timer.Start());
foreach (var bank in BankAcoutNumbers.BANS)
{
part.Add(bank);
if (++bansCounter >= BANS_PER_FILE)
{
string fileName = string.Format("{0}.txt-{1}.txt", part[0], part[part.Count - 1]);
string outputToFile = "";// Otherwise you dont see the lines in the file. Just single line!!
string subString = System.IO.Path.Combine(path, "nr");//Needed to add, because otherwise the files will not stored in the correct folder!!
fileName = subString + fileName;
foreach (var partBan in part)
{
using(StreamWriter st = new StreamWriter(fileName))
{
System.IO.File.WriteAllText(fileName, outputToFile);
}
//Console.WriteLine(partBan);
// outputToFile += partBan + Environment.NewLine;//Writing the lines to the file
}
;//Writes to file system.
part.Clear();
bansCounter = 0;
//System.IO.File.WriteAllText(fileName, part.ToString());
if (++fileCounter >= MAX_FILES)
break;
}
}
}
timer.Stop();
Console.WriteLine("Total time of writing the bank numbers to file system " + timer.Elapsed.Seconds + " seconds");
//Console.WriteLine(BankAcoutNumbers.BANS.Count());
}
catch (Exception)
{
throw;
}
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
我是这样尝试的:
foreach (var partBan 部分) { 使用(StreamWriter st = new StreamWriter(fileName)) {
System.IO.File.WriteAllText(fileName, outputToFile); } //Console.WriteLine(partBan); // outputToFile += partBan + Environment.NewLine;//Writing the lines to the file }
谢谢
我是这样尝试的:
foreach (var partBan in part)
{
using(StreamWriter st = new StreamWriter(fileName))
{
//System.IO.File.WriteAllText(subString, fileName);
st.WriteLine(outputToFile);
}
//Console.WriteLine(partBan);
// outputToFile += partBan + Environment.NewLine;//Writing the lines to the file
}
但我没有看到文件的内容。文件为空
我是这样尝试的:
public class Program
{
static void Main(string[] args)
{
string path = @"C:\BankNumber";
var bans = BankAcoutNumbers.BANS;
const int MAX_FILES = 80;
const int BANS_PER_FILE = 81818182 / 80;
int bansCounter = 0;
var part = new List<int>();
var maxNumberOfFiles = 10;
Stopwatch timer = new Stopwatch();
var fileCounter = 0;
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);
}
try
{
while (fileCounter <= maxNumberOfFiles)
{
timer.Start();
Console.WriteLine("Start writing the bank numbers to file system");
// Console.WriteLine(timer.Start());
foreach (var bank in BankAcoutNumbers.BANS)
{
part.Add(bank);
if (++bansCounter >= BANS_PER_FILE)
{
string fileName = string.Format("{0}.txt-{1}.txt", part[0], part[part.Count - 1]);
//string outputToFile = "";// Otherwise you dont see the lines in the file. Just single line!!
StringBuilder OutputFile = new StringBuilder("");
string subString = System.IO.Path.Combine(path, "nr");//Needed to add, because otherwise the files will not stored in the correct folder!!
fileName = subString + fileName;
foreach (var partBan in part)
{
OutputFile.Append(string.Format("{0}{1}", partBan.ToString(), Environment.NewLine));
//using(StreamWriter st = new StreamWriter(fileName))
//{
// //System.IO.File.WriteAllText(subString, fileName);
// StringBuilder strBuilder = new StringBuilder();
// strBuilder.Append(st.)
// st.Write(partBan + Environment.NewLine);
//}
//Console.WriteLine(partBan);
// outputToFile += partBan + Environment.NewLine;//Writing the lines to the file
}
//;//Writes to file system.
System.IO.File.WriteAllText(fileName, OutputFile.ToString());
part.Clear();
bansCounter = 0;
//System.IO.File.WriteAllText(fileName, part.ToString());
if (++fileCounter >= MAX_FILES)
break;
}
}
}
timer.Stop();
Console.WriteLine("Total time of writing the bank numbers to file system " + timer.Elapsed.TotalSeconds + " seconds");
//Console.WriteLine(BankAcoutNumbers.BANS.Count());
}
catch (Exception)
{
throw;
}
foreach (var item in part)
{
ThreadPool.QueueUserWorkItem(DoLongTask);
}
Console.WriteLine("Main thread ends");
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
public static void DoLongTask(object input)
{
Console.WriteLine("Thread is background : {0}", Thread.CurrentThread.IsBackground);
Console.WriteLine("Input parameter : {0}", input);
}
}
【问题讨论】:
标签: c#-5.0