c# 循环多次向txt文本中输入数据,使后一次的数据不覆盖上一次的数据
staticvoidgetTrainFeature(Stringsource_f,Stringfeature_f){StreamReaderreader=newStrea...
static void getTrainFeature(String source_f, String feature_f)
{
StreamReader reader = new StreamReader(source_f, System.Text.Encoding.Default);
String content = reader.ReadToEnd();
reader.Close();
String[] sentences = content.Split('\n');
for (int i = 0; i < sentences.Length; i++)
{
String[] sentences_part = sentences[i].Split(':');
String feature = sentences_part[0];
String f1 = "";//特征1:连续标点符号
String f2 = "";//特征2:第一第二人称+表达意见的动词
String f3 = "";//特征3:停用词
String f4 = "";//特征4:情感标点符号
f1 = Match1(sentences[i]);//得到特征1
f2 = Match2(sentences[i]);//得到特征2
f3 = Match3(sentences[i]);//得到特征3
f4 = Match4(sentences[i]);//得到特征4
String path = feature_f;
FileStream file = new FileStream(path, FileMode.Open);
StreamWriter read = new StreamWriter(file, Encoding.Default);
read.Write("\t" + f1 + "\t" + f2 + "\t" + f3 + "\t" + f4 + "\n");
read.Close();
file.Close();
}
}
以上代码我想通过循环,在txt文本中分行记录每次循环得到的值,可是我的source_f中无论有几行代码,生成的文本中都只有一行,请问怎么输出才能不覆盖呢 展开
{
StreamReader reader = new StreamReader(source_f, System.Text.Encoding.Default);
String content = reader.ReadToEnd();
reader.Close();
String[] sentences = content.Split('\n');
for (int i = 0; i < sentences.Length; i++)
{
String[] sentences_part = sentences[i].Split(':');
String feature = sentences_part[0];
String f1 = "";//特征1:连续标点符号
String f2 = "";//特征2:第一第二人称+表达意见的动词
String f3 = "";//特征3:停用词
String f4 = "";//特征4:情感标点符号
f1 = Match1(sentences[i]);//得到特征1
f2 = Match2(sentences[i]);//得到特征2
f3 = Match3(sentences[i]);//得到特征3
f4 = Match4(sentences[i]);//得到特征4
String path = feature_f;
FileStream file = new FileStream(path, FileMode.Open);
StreamWriter read = new StreamWriter(file, Encoding.Default);
read.Write("\t" + f1 + "\t" + f2 + "\t" + f3 + "\t" + f4 + "\n");
read.Close();
file.Close();
}
}
以上代码我想通过循环,在txt文本中分行记录每次循环得到的值,可是我的source_f中无论有几行代码,生成的文本中都只有一行,请问怎么输出才能不覆盖呢 展开
4个回答
展开全部
static void getTrainFeature(String source_f, String feature_f)
{
StreamReader reader = new StreamReader(source_f, System.Text.Encoding.Default);
String content = reader.ReadToEnd();
reader.Close();
String[] sentences = content.Split('\n');
String path = feature_f;
FileStream file = new FileStream(path, FileMode.Open);
StreamWriter read = new StreamWriter(file, Encoding.Default);
for (int i = 0; i < sentences.Length; i++)
{
String[] sentences_part = sentences[i].Split(':');
String feature = sentences_part[0];
String f1 = "";//特征1:连续标点符号
String f2 = "";//特征2:第一第二人称+表达意见的动词
String f3 = "";//特征3:停用词
String f4 = "";//特征4:情感标点符号
f1 = Match1(sentences[i]);//得到特征1
f2 = Match2(sentences[i]);//得到特征2
f3 = Match3(sentences[i]);//得到特征3
f4 = Match4(sentences[i]);//得到特征4
read.Write("\t" + f1 + "\t" + f2 + "\t" + f3 + "\t" + f4 + "\r\n");
}
read.Close();
file.Close();
}
{
StreamReader reader = new StreamReader(source_f, System.Text.Encoding.Default);
String content = reader.ReadToEnd();
reader.Close();
String[] sentences = content.Split('\n');
String path = feature_f;
FileStream file = new FileStream(path, FileMode.Open);
StreamWriter read = new StreamWriter(file, Encoding.Default);
for (int i = 0; i < sentences.Length; i++)
{
String[] sentences_part = sentences[i].Split(':');
String feature = sentences_part[0];
String f1 = "";//特征1:连续标点符号
String f2 = "";//特征2:第一第二人称+表达意见的动词
String f3 = "";//特征3:停用词
String f4 = "";//特征4:情感标点符号
f1 = Match1(sentences[i]);//得到特征1
f2 = Match2(sentences[i]);//得到特征2
f3 = Match3(sentences[i]);//得到特征3
f4 = Match4(sentences[i]);//得到特征4
read.Write("\t" + f1 + "\t" + f2 + "\t" + f3 + "\t" + f4 + "\r\n");
}
read.Close();
file.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要追加数据流,而不是重写数据流
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FileStream file = new FileStream(path,FileMode.Append);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询