您的位置:澳门新葡8455最新网站 > 编程教学 > 对文本的读写,读写文本文件

对文本的读写,读写文本文件

发布时间:2019-12-09 15:19编辑:编程教学浏览(50)

    今天急需对八个文书进行多少的划分,因为数据量宏大,所以就想到了经过写程序来处理。将代码贴出来以备今后使用。

    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);

    //读取文件的剧情 放置于StringBuilder 中

    }
    return sb.ToString();

    StreamReader sr = new StreamReader(path, Encoding.Default);
    String line;
    StringBuilder sb = new StringBuilder();

    }

    while ((line = sr.ReadLine()) != null)
    {
    sb.Append(line);
    }

     

     

     

    //然后将其装换为泛型对象 

    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 "";
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    Models.aa a = js.Deserialize<aa>(sb.ToString());
    List<aaa> aa = a.dateils;

     

    图片 1

    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;
    }
    }

    //然后在经过foreach来遍历泛型 平素到达钦定数量的分组 小说中分组的数量为100

     

    图片 2

     

    图片 3

     

    图片 4

     

     

     

    图片 5

     

     

     

    纪事:须求在web.config设置保存的路线 

     

     

     

     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;
                }
            }
    
        }
    

      

    本文由澳门新葡8455最新网站发布于编程教学,转载请注明出处:对文本的读写,读写文本文件

    关键词: