查看: 930|回复: 1
打印 上一主题 下一主题

[其他] txt读写操作封装

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-1-17 05:13:06 |只看该作者 |倒序浏览
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System;
  6. public class TextCreatWrite : MonoBehaviour {

  7.         /// <summary>
  8.         /// 判断一个文件是否存在.
  9.         /// </summary>
  10.         /// <returns><c>true</c>, if exists was filed, <c>false</c> otherwise.</returns>
  11.         /// <param name="path">Path.</param>
  12.         public static bool FileExists(string path)
  13.         {
  14.                 if(File.Exists(path))
  15.                         //存在
  16.                         return true;
  17.                 else
  18.                         //不存在
  19.                         return false;
  20.         }

  21.         /// <summary>
  22.         /// 创建一个文件,文件存在就不创建.
  23.         /// </summary>
  24.         /// <param name="path">Path.</param>
  25.         public static void CreatFile(string path)
  26.         {
  27.                 if (FileExists (path))   //文件已经存在就返回.
  28.                         return;
  29.                 // 如果文件不存在,创建文件; 如果存在,覆盖文件
  30.                 StreamWriter sw2 = new StreamWriter(path, false, Encoding.UTF8);
  31.                 sw2.Close ();
  32.         }

  33.         /// <summary>
  34.         /// 在文件末尾追加写入数据,然后在数据后面添加换行符.
  35.         /// </summary>
  36.         /// <param name="path">路径.</param>
  37.         /// <param name="date">要写入的字符串.</param>
  38.         public static void AppendTextAddLineFeed(string path,string date)
  39.         {
  40.                 // true 是 append text, false 为覆盖原文件
  41.                 StreamWriter sw2 = new StreamWriter(path, true, Encoding.UTF8);
  42.                 sw2.WriteLine (date);
  43.                 sw2.Close ();
  44.         }


  45.         /// <summary>
  46.         ///表示换行符的string.
  47.         /// </summary>
  48.         /// <returns>The line feed.</returns>
  49.         public static string GetLineFeed()
  50.         {
  51.                 //utf-8里换行的十六进制是 0d 0a
  52.                 //用转义字符表示\n\r
  53.                 int value1 = Convert.ToInt32("0D", 16);
  54.                 int value2 = Convert.ToInt32("0A", 16);
  55.                 string stringValue = Char.ConvertFromUtf32(value1);
  56.                 stringValue+=Char.ConvertFromUtf32(value2);
  57.                 return stringValue;
  58.         }

  59.         /// <summary>
  60.         /// 把16进制转成string格式.
  61.         /// </summary>
  62.         /// <returns>The tostring.</returns>
  63.         /// <param name="str16">要转换的16进制字符,比如"0d"是回车.</param>
  64.         static public string ConvertHex16To_string(string str16)
  65.         {
  66.                 int value1 = Convert.ToInt32(str16, 16);
  67.                 string stringValue = Char.ConvertFromUtf32(value1);
  68.                 return stringValue;
  69.         }

  70.         /// <summary>
  71.         /// 16进制转char.
  72.         /// </summary>
  73.         /// <returns>The to char.</returns>
  74.         /// <param name="str16">要转换的16进制字符,比如"0d"是回车".</param>
  75.         static public char ConvertHex16ToChar(string str16)
  76.         {
  77.                 int value1 = Convert.ToInt32(str16, 16);
  78.                 return (char)value1;
  79.         }

  80.         /// <summary>
  81.         /// 读取文件,返回每行数据集合List<string>.
  82.         /// </summary>
  83.         /// <returns>The all lines.</returns>
  84.         /// <param name="path">路径.</param>
  85.         static public List<string> ReadAllLines(string path)
  86.         {
  87.                 // 也可以指定编码方式
  88.                 string[] strs2 = File.ReadAllLines(path, Encoding.UTF8);
  89.                 return new List<string> (strs2);
  90.         }
  91. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-24 04:02 , Processed in 0.068325 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部