mirror of
https://e.coding.net/circlecloud/SEOKeywordSearch.git
synced 2024-11-14 00:48:48 +00:00
be29882c7d
Signed-off-by: j502647092 <jtb1@163.com>
55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
|
||
namespace AliKeywordSearch
|
||
{
|
||
class Config
|
||
{
|
||
protected string fileName;
|
||
public Config(string fileName)
|
||
{
|
||
this.fileName = fileName;
|
||
}
|
||
|
||
//将List转换为TXT文件
|
||
public void WriteListToTextFile(List<string> list)
|
||
{
|
||
//创建一个文件流,用以写入或者创建一个StreamWriter
|
||
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
|
||
StreamWriter sw = new StreamWriter(fs);
|
||
sw.Flush();
|
||
// 使用StreamWriter来往文件中写入内容
|
||
sw.BaseStream.Seek(0, SeekOrigin.Begin);
|
||
for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]);
|
||
//关闭此文件t
|
||
sw.Flush();
|
||
sw.Close();
|
||
fs.Close();
|
||
}
|
||
|
||
//读取文本文件转换为List
|
||
public List<string> ReadTextFileToList()
|
||
{
|
||
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
||
List<string> list = new List<string>();
|
||
StreamReader sr = new StreamReader(fs);
|
||
//使用StreamReader类来读取文件
|
||
sr.BaseStream.Seek(0, SeekOrigin.Begin);
|
||
// 从数据流中读取每一行,直到文件的最后一行
|
||
string tmp = sr.ReadLine();
|
||
while (tmp != null)
|
||
{
|
||
list.Add(tmp);
|
||
tmp = sr.ReadLine();
|
||
}
|
||
//关闭此StreamReader对象
|
||
sr.Close();
|
||
fs.Close();
|
||
return list;
|
||
}
|
||
}
|
||
}
|
||
|