| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- //************************************************************************
- // author: yuzhengyang
- // date: 2018.3.27 - 2018.6.3
- // desc: 工具描述
- // Copyright (c) yuzhengyang. All rights reserved.
- //************************************************************************
- using Azylee.Core.DataUtils.CollectionUtils;
- using Azylee.Core.IOUtils.DirUtils;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace Azylee.Core.IOUtils.TxtUtils
- {
- public class TxtTool
- {
- public static bool Append(string file, List<string> txt)
- {
- try
- {
- DirTool.Create(Path.GetDirectoryName(file));
- using (StreamWriter sw = new StreamWriter(file, true))
- {
- if (!ListTool.IsNullOrEmpty(txt))
- foreach (var t in txt)
- sw.WriteLine(t);
- }
- return true;
- }
- catch (Exception e) { }
- return false;
- }
- public static bool Append(string file, string txt)
- {
- try
- {
- DirTool.Create(Path.GetDirectoryName(file));
- using (StreamWriter sw = new StreamWriter(file, true))
- {
- sw.WriteLine(txt);
- }
- return true;
- }
- catch (Exception e) { }
- return false;
- }
- public static bool Create(string file, string txt)
- {
- try
- {
- DirTool.Create(Path.GetDirectoryName(file));
- using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8))
- {
- sw.WriteLine(txt);
- }
- return true;
- }
- catch (Exception e) { }
- return false;
- }
- public static string Read(string file)
- {
- try
- {
- if (File.Exists(file))
- {
- using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
- {
- string result = "", line;
- while ((line = sr.ReadLine()) != null)
- result += line.ToString();
- return result;
- }
- }
- }
- catch { }
- return null;
- }
- public static List<string> ReadLine(string file)
- {
- try
- {
- using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
- {
- List<string> result = new List<string>();
- string line;
- while ((line = sr.ReadLine()) != null)
- result.Add(line.ToString());
- return result;
- }
- }
- catch (Exception e) { }
- return null;
- }
- public static void ReadLine(string file, Action<int, string> action)
- {
- try
- {
- using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
- {
- string line;
- int number = 1;
- while ((line = sr.ReadLine()) != null)
- action.Invoke(number++, line);
- }
- }
- catch (Exception e) { }
- }
- public static void ReadLine(string file,Encoding encoding, Action<int, string> action)
- {
- try
- {
- using (StreamReader sr = new StreamReader(file, encoding))
- {
- string line;
- int number = 1;
- while ((line = sr.ReadLine()) != null)
- action.Invoke(number++, line);
- }
- }
- catch (Exception e) { }
- }
- public static long CountLine(string file, string[] filter)
- {
- long count = 0;
- try
- {
- using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
- {
- string line;
- while ((line = sr.ReadLine()) != null)
- {
- bool access = true;
- if (!ListTool.IsNullOrEmpty(filter))
- {
- foreach (var f in filter)
- {
- if (line.Trim() == f) access = false;
- }
- }
- if (access) count++;
- }
- }
- }
- catch (Exception e) { }
- return count;
- }
- }
- }
|