数组写入到TXT:
if (!File.Exists("E:\\a.txt")) { FileStream FsCreate = new FileStream("E:\\a.txt", FileMode.Create); FsCreate.Close(); }
FileStream fs = new FileStream("E:\\a.txt", FileMode.Append); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("strtaszfdsadfasdfadfsadf输入文本"); sw.Close(); fs.Close();
确认一下:
using System; using System.Collections.Generic; using System.Text; using System.IO;
把 TXT文本 读入一个 string[] 数组中:
public string[] TxtToArray(string full_path) { if (File.Exists(full_path)) { StreamReader sr = new StreamReader(full_path, Encoding.UTF8); string[] content_array = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None); sr.Close(); return content_array; } else { return new string[1]; } }
把 TXT文本 读入一个 string[] 数组中,并删除内容为 line_content 的元素:
public string[] TxtDeleteLine(string full_path, string line_content) { if (File.Exists(full_path)) { StreamReader sr = new StreamReader(full_path, Encoding.UTF8); List<string> content_list = new List<string>(sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None)); sr.Close(); for (int i = content_list.Count - 1; i >= 0; i--) { if (content_list[i] == line_content) { content_list.RemoveAt(i); } } return content_list.ToArray(); } else { return new string[1]; } }
把 TXT文本 读入一个 string[] 数组中,并删除内容包含在 line_content_array 中的元素:
public string[] TxtDeleteMultiLine(string full_path, string[] line_content_array) { if (File.Exists(full_path)) { StreamReader sr = new StreamReader(full_path, Encoding.UTF8); string content = sr.ReadToEnd(); sr.Close(); List<string> content_list = new List<string>(content.Split(new string[] { "\r\n" }, StringSplitOptions.None)); for (int i = content_list.Count - 1; i >= 0; i--) { foreach (string line in line_content_array) { if (content_list[i] == line) { content_list.RemoveAt(i); break; } } } return content_list.ToArray(); } else { return new string[1]; } }
获取两个TXT文件的差集 string[] 数组,即包含不同的行的数组:
public string[] TxtDiff(string full_path_1, string full_path_2) { if (File.Exists(full_path_1) && File.Exists(full_path_2)) { StreamReader sr_1 = new StreamReader(full_path_1, Encoding.UTF8); string[] content_array_1 = sr_1.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None); sr_1.Close(); StreamReader sr_2 = new StreamReader(full_path_2, Encoding.UTF8); string[] content_array_2 = sr_2.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None); sr_2.Close(); List<string> total_list = new List<string>(); total_list.AddRange(content_array_1); total_list.AddRange(content_array_2); total_list.Sort(); List<string> same_list = new List<string>(); for (int i = 1; i < total_list.Count; i++) { if (total_list[i] == total_list[i - 1]) { same_list.Add(total_list[i]); total_list.RemoveAt(i); i--; } } foreach (string line in same_list) { for (int j = 0; j < total_list.Count; j++) { if (total_list[j] == line) { total_list.RemoveAt(j); j--; } } } return total_list.ToArray(); } else { return new string[1]; } }
获取两个TXT文件的交集 string[] 数组,即包含相同的行的数组:
public string[] TxtSame(string full_path_1, string full_path_2) { if (File.Exists(full_path_1) && File.Exists(full_path_2)) { StreamReader sr_1 = new StreamReader(full_path_1, Encoding.UTF8); string[] content_array_1 = sr_1.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None); sr_1.Close(); StreamReader sr_2 = new StreamReader(full_path_2, Encoding.UTF8); string[] content_array_2 = sr_2.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None); sr_2.Close(); List<string> total_list = new List<string>(); total_list.AddRange(content_array_1); total_list.AddRange(content_array_2); total_list.Sort(); List<string> same_list = new List<string>(); for (int i = 1; i < total_list.Count; i++) { if (total_list[i] == total_list[i - 1]) { same_list.Add(total_list[i]); total_list.RemoveAt(i); i--; } } for (int j = 1; j < same_list.Count; j++) { if (same_list[j] == same_list[j - 1]) { same_list.RemoveAt(j); j--; } } return same_list.ToArray(); } else { return new string[1]; } }