C# 在C语言中读/写txt文件#
这里是初学者 我很难理解如何在c#中编辑txt文件的内容。我正在尝试执行以下操作-伪代码:C# 在C语言中读/写txt文件#,c#,streamreader,streamwriter,C#,Streamreader,Streamwriter,这里是初学者 我很难理解如何在c#中编辑txt文件的内容。我正在尝试执行以下操作-伪代码: foreach word in file.txt if ((word.length < 4) || (word.length > 11)) delete word from file.txt file.txt中的每个单词 如果((字长11)) 从file.txt中删除word 我需要做什么?我知道它涉及到streamreade
foreach word in file.txt
if ((word.length < 4) || (word.length > 11))
delete word from file.txt
file.txt中的每个单词
如果((字长<4)| |(字长>11))
从file.txt中删除word
我需要做什么?我知道它涉及到streamreader/writer类,但我不知道它们是如何工作的。这个概念将更接近于:
While(there is input to read from the input file)
{
read the input
if(input fits your criteria of shorter than 4 or longer than 11)
ignore it
else
write it to output file (which is a new file, NOT the file you read it from)
}
您可以使用这个概念将更符合以下内容:
While(there is input to read from the input file)
{
read the input
if(input fits your criteria of shorter than 4 or longer than 11)
ignore it
else
write it to output file (which is a new file, NOT the file you read it from)
}
您可以使用这个概念将更符合以下内容:
While(there is input to read from the input file)
{
read the input
if(input fits your criteria of shorter than 4 or longer than 11)
ignore it
else
write it to output file (which is a new file, NOT the file you read it from)
}
您可以使用这个概念将更符合以下内容:
While(there is input to read from the input file)
{
read the input
if(input fits your criteria of shorter than 4 or longer than 11)
ignore it
else
write it to output file (which is a new file, NOT the file you read it from)
}
您可以使用根据您在问题中描述的要求,我会研究regex来进行模式匹配: 这里有一个关于正则表达式的好教程。以单词为目标,用空格代替 结合下面关于如何读/写文本文件的文章。 根据文件的大小,您可以只读取整个文件,删除要删除的单词,最后写回整个内容。
如果文件非常大,您可能需要对此进行优化,并将文件分块读取。我将研究正则表达式,根据您在问题中描述的要求进行模式匹配: 这里有一个关于正则表达式的好教程。以单词为目标,用空格代替 结合下面关于如何读/写文本文件的文章。 根据文件的大小,您可以只读取整个文件,删除要删除的单词,最后写回整个内容。
如果文件非常大,您可能需要对此进行优化,并将文件分块读取。我将研究正则表达式,根据您在问题中描述的要求进行模式匹配: 这里有一个关于正则表达式的好教程。以单词为目标,用空格代替 结合下面关于如何读/写文本文件的文章。 根据文件的大小,您可以只读取整个文件,删除要删除的单词,最后写回整个内容。
如果文件非常大,您可能需要对此进行优化,并将文件分块读取。我将研究正则表达式,根据您在问题中描述的要求进行模式匹配: 这里有一个关于正则表达式的好教程。以单词为目标,用空格代替 结合下面关于如何读/写文本文件的文章。 根据文件的大小,您可以只读取整个文件,删除要删除的单词,最后写回整个内容。 如果文件非常大,您可能需要对此进行优化,并将文件分块读取。试试这个
var filePath=HttpRuntime.AppDomainAppPath+“您的文件路径”;
如果(!File.Exists(filePath))
返回;
使用(var sr=newstreamreader(filePath))
{
var text=sr.ReadToEnd();
如果(text.Length<4 | | text.Length>11)
{
使用(var sw=新StreamWriter(文件路径))
{
sw.写(“”);
}
}
}
var filePath=HttpRuntime.AppDomainAppPath+“您的文件路径”;
如果(!File.Exists(filePath))
返回;
使用(var sr=newstreamreader(filePath))
{
var text=sr.ReadToEnd();
如果(text.Length<4 | | text.Length>11)
{
使用(var sw=新StreamWriter(文件路径))
{
sw.写(“”);
}
}
}
var filePath=HttpRuntime.AppDomainAppPath+“您的文件路径”;
如果(!File.Exists(filePath))
返回;
使用(var sr=newstreamreader(filePath))
{
var text=sr.ReadToEnd();
如果(text.Length<4 | | text.Length>11)
{
使用(var sw=新StreamWriter(文件路径))
{
sw.写(“”);
}
}
}
var filePath=HttpRuntime.AppDomainAppPath+“您的文件路径”;
如果(!File.Exists(filePath))
返回;
使用(var sr=newstreamreader(filePath))
{
var text=sr.ReadToEnd();
如果(text.Length<4 | | text.Length>11)
{
使用(var sw=新StreamWriter(文件路径))
{
sw.写(“”);
}
}
}
乍一看,使用StreamReader读取文件、拆分空间,然后删除不符合长度标准的单词似乎很简单。然后使用StreamWriter将结果写回。但是,使用字符串解析(单词解析)时,您会遇到一系列“特殊”情况,其中会出现额外的处理
/// <summary>
/// Removes words from a file
/// </summary>
/// <param name="filePath">the file path to parse</param>
/// <param name="preservePuncuation">flag to preserve the puncation for rebuilding the string</param>
/// <param name="minWordLength">the minimum word length</param>
/// <param name="maxWordLength">the maximum word length</param>
public static void RemoveWordsFromAFile(string filePath, bool preservePuncuation, int minWordLength, int maxWordLength)
{
//our parsed string
string parseString = "";
//read the file
using (var reader = new StreamReader(filePath))
{
parseString = reader.ReadToEnd();
}
//open a new writer
using (var writer = new StreamWriter(filePath))
{
//parse our string to remove words
parseString = WordProcessor.RemoveWords(parseString, preservePuncuation, minWordLength, maxWordLength);
//write our string
writer.Write(parseString);
writer.Flush();
}
}
WordProcessor.RemoveWordsFromAFile(@"D:\test.txt", true, 4, 10);