bwin必赢棋牌:读写文本文件,对文件的读写

bwin必赢棋牌 3

bwin必赢棋牌 1

 

 

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

    }

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

public static string ReadFile(string FilePath)
{
if (System.IO.File.Exists(FilePath))
{
bwin必赢棋牌,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 “”;
}

bwin必赢棋牌 2

 

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

 

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

}

 

 

bwin必赢棋牌 3

 

 

 

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

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注