zoukankan      html  css  js  c++  java
  • 第二节:师傅延伸的一些方法(复习_总结)

    一丶读写文件帮助类:转自我师父----->www.cnblogs.com/yaopengfei(更多内容,请关注)

      1 /// <summary>
      2     /// 文件操作类
      3     /// 特别注意:
      4     /// 1.在非web程序中,HttpContext.Current.Server.MapPath失效不好用,需要使用System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
      5     ///   获取本目录,然后写到根目录里。AppDomain.CurrentDomain.BaseDirectory将获取到Debug文件夹,无法使用相对路径
      6     /// 2.在web程序里,可以使用HttpContext.Current.Server.MapPath进行转换,使用方法通过 ~/定位到一级目录,eg:FileOperateHelp.Write_Txt("~/TestFile/mr.txt", "mr");
      7     /// </summary>
      8     public static class FileOperateHelp
      9     {
     10 
     11         #region 01.写文件(.txt-覆盖)
     12         /// <summary>
     13         /// 写文件(覆盖源文件内容)
     14         /// 文件不存在的话自动创建
     15         /// </summary>
     16         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>
     17         /// <param name="Content">文件内容</param>
     18         public static string Write_Txt(string FileName, string Content)
     19         {
     20             try
     21             {
     22                 Encoding code = Encoding.GetEncoding("gb2312");
     23                 string htmlfilename = FileOperateHelp.PathConvert(FileName);
     24                 //string htmlfilename = HttpContext.Current.Server.MapPath(FileName + ".txt"); //保存文件的路径  
     25                 string str = Content;
     26                 StreamWriter sw = null;
     27                 {
     28                     try
     29                     {
     30                         sw = new StreamWriter(htmlfilename, false, code);
     31                         sw.Write(str);
     32                         sw.Flush();
     33                     }
     34                     catch { }
     35                 }
     36                 sw.Close();
     37                 sw.Dispose();
     38                 return "ok";
     39             }
     40             catch (Exception ex)
     41             {
     42 
     43                 return ex.Message;
     44             }
     45 
     46         }
     47         #endregion
     48 
     49         #region 02.读文件(.txt)
     50         /// <summary>
     51         /// 读文件
     52         /// </summary>
     53         /// <param name="filename">文件路径(web里相对路径,控制台在根目录下写)</param>
     54         /// <returns></returns>
     55         public static string Read_Txt(string filename)
     56         {
     57 
     58             try
     59             {
     60                 Encoding code = Encoding.GetEncoding("gb2312");
     61                 string temp = FileOperateHelp.PathConvert(filename);
     62                 //  string temp = HttpContext.Current.Server.MapPath(filename + ".txt");
     63                 string str = "";
     64                 if (File.Exists(temp))
     65                 {
     66                     StreamReader sr = null;
     67                     try
     68                     {
     69                         sr = new StreamReader(temp, code);
     70                         str = sr.ReadToEnd(); // 读取文件  
     71                     }
     72                     catch { }
     73                     sr.Close();
     74                     sr.Dispose();
     75                 }
     76                 else
     77                 {
     78                     str = "";
     79                 }
     80                 return str;
     81             }
     82             catch (Exception ex)
     83             {
     84                 
     85                   return ex.Message;
     86             }
     87         }
     88         #endregion
     89 
     90         #region 03.写文件(.txt-添加)
     91         /// <summary>  
     92         /// 写文件  
     93         /// </summary>  
     94         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>  
     95         /// <param name="Strings">文件内容</param>  
     96         public static string WriteFile(string FileName, string Strings)
     97         {
     98             try
     99             {
    100                 string Path = FileOperateHelp.PathConvert(FileName);
    101 
    102                 if (!System.IO.File.Exists(Path))
    103                 {
    104                     System.IO.FileStream f = System.IO.File.Create(Path);
    105                     f.Close();
    106                     f.Dispose();
    107                 }
    108                 System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
    109                 f2.WriteLine(Strings);
    110                 f2.Close();
    111                 f2.Dispose();
    112                 return "ok";
    113             }
    114             catch (Exception ex)
    115             {
    116 
    117                 return ex.Message;
    118             }
    119         }
    120         #endregion
    121 
    122         #region 04.读文件(.txt)
    123         /// <summary>  
    124         /// 读文件  
    125         /// </summary>  
    126         /// <param name="FileName">文件路径(web里相对路径,控制台在根目录下写)</param>  
    127         /// <returns></returns>  
    128         public static string ReadFile(string FileName)
    129         {
    130             try
    131             {
    132                 string Path = FileOperateHelp.PathConvert(FileName);
    133                 string s = "";
    134                 if (!System.IO.File.Exists(Path))
    135                     s = "不存在相应的目录";
    136                 else
    137                 {
    138                     StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
    139                     s = f2.ReadToEnd();
    140                     f2.Close();
    141                     f2.Dispose();
    142                 }
    143                 return s;
    144             }
    145             catch (Exception ex)
    146             {
    147                 return ex.Message;
    148             }
    149         }
    150         #endregion
    151 
    152         #region 05.删除文件
    153         /// <summary>  
    154         /// 删除文件  
    155         /// </summary>  
    156         /// <param name="Path">文件路径(web里相对路径,控制台在根目录下写)</param>  
    157         public static string FileDel(string Path)
    158         {
    159             try
    160             {
    161                 string temp = FileOperateHelp.PathConvert(Path);
    162                 File.Delete(temp);
    163                 return "ok";
    164             }
    165             catch (Exception ex)
    166             {
    167                 return ex.Message;
    168             }
    169         }
    170         #endregion
    171 
    172         #region 06.移动文件
    173         /// <summary>  
    174         /// 移动文件  
    175         /// </summary>  
    176         /// <param name="OrignFile">原始路径(web里相对路径,控制台在根目录下写)</param>  
    177         /// <param name="NewFile">新路径,需要写上路径下的文件名,不能单写路径(web里相对路径,控制台在根目录下写)</param>  
    178         public static string FileMove(string OrignFile, string NewFile)
    179         {
    180             try
    181             {
    182                 OrignFile = FileOperateHelp.PathConvert(OrignFile);
    183                 NewFile = FileOperateHelp.PathConvert(NewFile);
    184                 File.Move(OrignFile, NewFile);
    185                 return "ok";
    186             }
    187             catch (Exception ex)
    188             {
    189                 return ex.Message;
    190             }
    191         }
    192         #endregion
    193 
    194         #region 07.复制文件
    195         /// <summary>  
    196         /// 复制文件  
    197         /// </summary>  
    198         /// <param name="OrignFile">原始文件(web里相对路径,控制台在根目录下写)</param>  
    199         /// <param name="NewFile">新文件路径(web里相对路径,控制台在根目录下写)</param>  
    200         public static string FileCopy(string OrignFile, string NewFile)
    201         {
    202             try
    203             {
    204                 OrignFile = FileOperateHelp.PathConvert(OrignFile);
    205                 NewFile = FileOperateHelp.PathConvert(NewFile);
    206                 File.Copy(OrignFile, NewFile, true);
    207                 return "ok";
    208             }
    209             catch (Exception ex)
    210             {
    211                 return ex.Message;
    212             }
    213         }
    214         #endregion
    215         
    216         #region 08.创建文件夹
    217         /// <summary>  
    218         /// 创建文件夹  
    219         /// </summary>  
    220         /// <param name="Path">相对路径(web里相对路径,控制台在根目录下写)</param>  
    221         public static string FolderCreate(string Path)
    222         {
    223             try
    224             {
    225                 Path = FileOperateHelp.PathConvert(Path);
    226                 // 判断目标目录是否存在如果不存在则新建之  
    227                 if (!Directory.Exists(Path))
    228                 {
    229                     Directory.CreateDirectory(Path);
    230                 }
    231                 return "ok";
    232             }
    233             catch (Exception ex)
    234             {
    235                 return ex.Message;
    236             }
    237         }
    238         #endregion
    239 
    240         #region 09.递归删除文件夹目录及文件
    241         /// <summary>  
    242         /// 递归删除文件夹目录及文件  
    243         /// </summary>  
    244         /// <param name="dir">相对路径(web里相对路径,控制台在根目录下写) 截止到哪删除到哪,eg:/a/ 连a也删除</param>    
    245         /// <returns></returns>  
    246         public static string DeleteFolder(string dir)
    247         {
    248 
    249             try
    250             {
    251                 string adir = FileOperateHelp.PathConvert(dir);
    252                 if (Directory.Exists(adir)) //如果存在这个文件夹删除之   
    253                 {
    254                     foreach (string d in Directory.GetFileSystemEntries(adir))
    255                     {
    256                         if (File.Exists(d))
    257                             File.Delete(d); //直接删除其中的文件                          
    258                         else
    259                             DeleteFolder(d); //递归删除子文件夹   
    260                     }
    261                     Directory.Delete(adir, true); //删除已空文件夹                   
    262                 }
    263                 return "ok";
    264             }
    265             catch (Exception ex)
    266             {
    267                 return ex.Message;
    268             }
    269         }
    270 
    271         #endregion
    272 
    273         #region 10.将相对路径转换成绝对路径
    274         /// <summary>
    275         /// 10.将相对路径转换成绝对路径
    276         /// </summary>
    277         /// <param name="strPath">相对路径</param>
    278         public static string PathConvert(string strPath)
    279         {
    280             //web程序使用
    281             if (HttpContext.Current != null)
    282             {
    283                 return HttpContext.Current.Server.MapPath(strPath);
    284             }
    285             else //非web程序引用             
    286             {
    287                 strPath = strPath.Replace("/", "\");
    288                 if (strPath.StartsWith("\"))
    289                 {
    290                     strPath = strPath.TrimStart('\');
    291                 }
    292                 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
    293             }
    294         }
    295         #endregion
    296 
    297 
    298 
    299     }
    View Code

    调用:

    1 try{
    2 int.Parse("dfdf");
    3 }catch (Exception ex){
    4  FileOperateHelp.WriteFile("E:/ErrorLog333.txt", ex.Message);
    5 }
    测试

    二丶.单例模式

     1 public class STwo
     2     {
     3         /// <summary>
     4         /// 模拟耗时的构造函数
     5         /// </summary>
     6         private STwo()
     7         {
     8             long result = 0;
     9             for (int i = 0; i < 1000000; i++)
    10             {
    11                 result += i;
    12             }
    13             Thread.Sleep(1000);
    14             Console.WriteLine("{0}被构造...", this.GetType().Name);
    15         }
    16 
    17         private static STwo _STwo = null;
    18         /// <summary>
    19         /// 静态的构造函数:只能有一个,且是无参数的
    20         /// 由CLR保证,只有在程序第一次使用该类之前被调用,而且只能调用一次
    21         /// </summary>
    22         static STwo()
    23         {
    24             _STwo = new STwo();
    25         }
    26 
    27         public static STwo CreateIntance()
    28         {
    29             return _STwo;
    30         }
    31     }
    1.静态构造函数
     1 public  class SThird
     2     {
     3         /// <summary>
     4         /// 模拟耗时的构造函数
     5         /// </summary>
     6         private SThird()
     7         {
     8             long result = 0;
     9             for (int i = 0; i < 1000000; i++)
    10             {
    11                 result += i;
    12             }
    13             Thread.Sleep(1000);
    14             Console.WriteLine("{0}被构造...", this.GetType().Name);
    15         }
    16         /// <summary>
    17         /// 静态变量:由CLR保证,在程序第一次使用该类之前被调用,而且只调用一次
    18         /// </summary>
    19         private static SThird _SThird = new SThird();
    20 
    21         public static SThird CreateIntance()
    22         {
    23             return _SThird;
    24         }
    25     }
    2.静态变量

    单例模式三种写法:双if+Lock(懒汉模式)、静态构造函数(饿汉模式)、静态变量(饿汉模式)详情---->https://www.cnblogs.com/yaopengfei/p/7138700.html

    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    java中使用静态字段和构造函数跟踪某个类所创建对象的个数
    java中静态初始化块的执行顺序
    Java字段初始化的规律
    java web第一次课堂测试1
    java课极限测试
    对于java中反编译命令的使用以及Integer包装类的查看
    在java的静态方法中访问类的实例成员
    [Android开发] 获取Android的Google Map API密钥
    [Android开发] 整合不同版本的android project的方法
    比特,字节和像素之间的关系
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/9272648.html
Copyright © 2011-2022 走看看