C# 如何在编写文件时声明文件大小限制
我是C#的新手,想问一下如何将特定的文件限制设置为逗号分隔的文件。我将使用它们的场景如下C# 如何在编写文件时声明文件大小限制,c#,C#,我是C#的新手,想问一下如何将特定的文件限制设置为逗号分隔的文件。我将使用它们的场景如下 const int sizeLimitInBytes = 500*1024*1024; using (var stream = new FileStream("data.csv", FileMode.Append)) using (var writer = new StreamWriter(stream)) { // if the file is newly created then write
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
if (!File.Exists(newFileName))
{
File.WriteAllText(newFileName, clientheader);
//File.WriteAllText(logFileName, logfileheader);
}
else
{
File.AppendAllText(newFileName, clientheader);
// File.AppendAllText(logFileName, logfileheader);
}
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
您可以使用流(FileStream)来写入csv数据,并通过检查流的Position属性来检查当前大小。假设allData数组中有要写入csv文件的数据(例如名称、年龄等),则其工作原理如下:
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
使用FileMode.Append写入文件末尾(如果存在)。否则它将创建一个
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
StreamWriter使用缓冲(缓冲区大小为4 KB),因此检查是否超过了限制并不完全准确,但对于500 MB的限制来说,这并不重要
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
如果您希望将所有内容一次性写入文件,则可以使用MemoryStream而不是FileStream,然后使用file.writealBytes将流中的字节转储到文件中。我遇到了类似的问题,创建的文件仅包含有限的字符。我找到了解决方案,并在创建文件时尝试使用关键字。现在,文件以最大大小创建
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}
using(StreamWriter sw = new StreamWriter(fs))
{
ArrayList chartList = GetChart(maintNode);
foreach (var line in chartList)
{
sw.WriteLine(line);
}
}
发件人:查看
文件。对于问题的第一部分,存在:。在跟踪您写入的数据量方面,一个简单的方法是统计您写入的字符数,因为每个字符有两个字节。非常感谢。您已经有了哪些用于写入CSV文件的代码?首先将其添加到您的问题中。@Noelskie,将您正在使用的代码添加到原始问题中,而不是添加到注释中。请参见此处:以字节为单位计算字符串的长度。您能详细说明一下这个答案吗。根据你提供的答案,我不明白。你能举个例子说明你是怎么解决的吗?这个答案并没有回答OP的问题。
const int sizeLimitInBytes = 500*1024*1024;
using (var stream = new FileStream("data.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
{
// if the file is newly created then write the csv column names in the first line
if (stream.Position == 0)
{
writer.WriteLine("Name, Age, Job, Address");
}
foreach (var currentData in allData)
{
if (stream.Position > sizeLimitInBytes)
{
break;
}
writer.WriteLine("..."); // write data seperated by commas
}
}