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

前不久亟待对1个文书举行多少的划分,因为数据量庞大,所以就想到了通过写程序来处理。将代码贴出来以备以后选取。

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

  开头查资料以为是io读取的,磁盘速度慢的等等的,后来一想,text
又不是几九千0条数据,才那么3000行,咋回事。

internal class Program
{
private static void Main(string[] args)
{

//读取文件的内容 放置于StringBuilder 中

}
return sb.ToString();

原来是循环中不停的改动textbox值导致的。

var content =
Read(@”E:\workspace\日志\2016-06-21.LOG2016-06-21.LOG”);
Regex reg3 = new Regex(@”手机号(?<result>(\d+)?)”);
var matches = reg3.Matches(content);
if (matches.Count > 0)
{
var list = new List<string>();
string phone;
foreach (Match item in matches)
{
phone = item.Result(“${result}”);
if (list.Contains(phone))
{

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

}

总结:

}
else
{
list.Add(phone);
}
}
读写文本文件,对文本的读写。}
}

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

 

 相比大的演算,特别是大的大循环,不要操作控件,因为控件的有的质量每变动2次,都要paint重绘2遍,那样作用会极低
诚如用变量来操作,然后完了一回就举行掉。

public static 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 “”;
}

string file = fileDialog.FileName;
//删除后缀 存入files类Filename属性 ,后用于存在word第1行

}
}

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

 

StreamReader sr = new StreamReader(file,
System.Text.Encoding.Default);
String line;
StringBuilder sb = new StringBuilder();
// int n = 0;
while (sr.Peek() > -1)
{

亚洲必赢官网 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;
}
}

//显示在textbox中
sb.Append(line + “\r\n”);
//text博克斯2.Text = sb.ToString();   //sb
在循环中向 texbox2
发送数据,每回发送,textbox控件都要刷新一遍,导致程序功用直线下挫。
//跳过行空格
if (line.Trim() != “”)
{
……
if (lines.Length >= 3)
{

//然后在通过foreach来遍历泛型 一直达到钦命数量的分组
小说中分组的数目为100

 

//数据传入数组然后入队列
……
}
}
}

亚洲必赢官网 2

 

r.Close(); sr.Dispose();
textBox2.Text =
sb.ToString(); //放在此地难点一蹴即至
}
}

亚洲必赢官网 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;
            }
        }

    }

  

网站地图xml地图