zoukankan      html  css  js  c++  java
  • C#读写文本文件

    static public string Read(string path)
    {
    StreamReader sr = new StreamReader(path,Encoding.Default);
    String line;
    StringBuilder sb=new StringBuilder();
    while ((line = sr.ReadLine()) != null)
    {
    sb.AppendLine(line);

    }
    return sb.ToString();

    }

    public static string ReadFile(string FilePath)
    {
    if (System.IO.File.Exists(FilePath))
    {
    try
    {
    StreamReader reader = new StreamReader(FilePath, Encoding.GetEncoding("gb2312"));//System.IO.File.OpenText(FilePath);
    StringBuilder builder = new StringBuilder();
    string str = "";
    string str2 = "";
    while ((str = reader.ReadLine()) != null)
    {
    builder.AppendLine(str  );
    }
    str2 = builder.ToString();
    reader.Close();
    return str2;
    }
    catch
    {
    return "";
    }
    }
    return "";
    }

    public static bool SaveFile(string FilePath, string type, string content)
    {
    string path = FilePath;
    if (!File.Exists(path))
    type = "NEW";
    try
    {
    StreamWriter writer;
    if (type == "NEW")
    {
    using (writer = new StreamWriter(FilePath, false, Encoding.GetEncoding("gb2312")))//System.IO.File.CreateText(path))
    {
    writer.Write(content);
    writer.Flush();
    writer.Close();
    return true;
    }
    }
    if (type == "ADD")
    {
    if (System.IO.File.Exists(path))
    {
    writer = new StreamWriter(path, true, Encoding.GetEncoding("gb2312"));
    writer.Write(content);
    writer.Flush();
    writer.Close();
    return true;
    }
    return false;
    }
    return false;
    }
    catch
    {
    return false;
    }
    }

     public class FileHelper
        {
            public static string ReadFile(string path)
            {
                StreamReader sr = new StreamReader(path, Encoding.Default);
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
    
                }
                return sb.ToString();
    
            }
    
            public static IEnumerable<string> ReadFileToEnumerable(string path)
            {
                StreamReader sr = new StreamReader(path, Encoding.Default);
                String line;
    
                while ((line = sr.ReadLine()) != null)
                {
                    yield return line;
    
                }
    
    
            }
    
            public static IList<T> ReadFileToList<T>(string[] path, Func<string, T> func)
            {
                var list = new List<T>();
                foreach (var item in path)
                {
                    list = list.Union(ReadFileToEnumerable(item).Select(func)).ToList();
                }
                return list;
            }
            public static bool AppendFile(string filePath, string type, string content)
            {
              
                try
                {
    
                    if (System.IO.File.Exists(filePath))
                        {
                            var writer = new StreamWriter(filePath, true, Encoding.GetEncoding("gb2312"));
                            writer.Write(content);
                            writer.Flush();
                            writer.Close();
                            return true;
                        }
                        return false;
                    
                }
                catch
                {
                    return false;
                }
            }
    
            public static bool CreateFile(string filePath, string type, string content)
            {
              
                try
                {
                    StreamWriter writer;
                    
                        using (writer = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312")))//System.IO.File.CreateText(path))
                        {
                            writer.Write(content);
                            writer.Flush();
                            writer.Close();
                            return true;
                        }
                   
                }
                catch
                {
                    return false;
                }
            }
    
        }
    

      

  • 相关阅读:
    iOS Runtime 实践(1)
    支付宝接口使用文档说明 支付宝异步通知
    ASP.NET中数据棒图,饼图,柱状图的实现
    ASP.NET运行机制之一般处理程序(ashx)
    ASP.NET的一套笔试题
    ASP.NET页面优化性能提升方法记录
    ASP.NET 大文件上传的简单处理
    ASP.NET输出PNG图片时出现GDI+一般性错误的解决方法
    您可能不知道的ASP.Net小技巧
    cache应用(asp.net 2.0 SQL数据缓存依赖 [SqlCacheDependency ] )
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5603221.html
Copyright © 2011-2022 走看看