zoukankan      html  css  js  c++  java
  • c#简单的io

    读取路径判断文件是否存在,进行删除或者创建

    简单的io

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
     
    namespace ComprehensiveTest.com.myio
    {
        public class IoManager
        {
            private static IoManager instance = null;
     
            public static IoManager Instance
            {
                get {
                    if (IoManager.instance == null)
                    {
                        IoManager.instance = new IoManager();
                    }
                    return IoManager.instance; 
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public bool CreateFile(string targetPath)
            {
                if (File.Exists(targetPath))
                {
                    return true;
                }
                else
                {
                    try
                    {
                        //使用这2种方法都可以
                        //FileStream file = File.Create(targetPath);
                        FileStream file = new FileStream(targetPath, FileMode.Create);
                        file.Close();
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("创建文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
                        return false;
                    }
                }
            }
            /// <summary>
            /// 获得电脑所有的驱动盘
            /// </summary>
            /// <returns></returns>
            public string[] GetMyLogicalDrives()
            {
                return Directory.GetLogicalDrives();
            }
            /// <summary>
            /// 移动数据
            /// </summary>
            /// <param name="oldPath"> 原始的路径  </param>
            /// <param name="newPath"> 新的路径 </param>
            /// <returns> 操作是否成功 </returns>
            public bool MoveFile(string oldPath, string newPath)
            {
                if (File.Exists(oldPath))
                {
                    try
                    {
                        File.Move(oldPath, newPath);
                        return true;
                    }
                    catch (Exception e)
                    { 
                        Console.WriteLine("移动文件{0},失败 , 原因 : {1} " , oldPath , e.ToString() );
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("Error , {0}文件不存在!!! " , oldPath );
                    return false;
                }
            }
            /// <summary>
            /// 复制一个文件
            /// </summary>
            /// <param name="oldPath"></param>
            /// <param name="newPath"></param>
            /// <returns></returns>
            public bool CopyFile(string oldPath, string newPath)
            {
                if (File.Exists(oldPath))
                {
                    try
                    {
                        File.Copy(oldPath, newPath);
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("复制文件{0},失败 , 原因 : {1} ", oldPath, e.ToString());
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("Error , {0}文件不存在!!! ", oldPath);
                    return false;
                }
            }
            /// <summary>
            /// 删除一个文件
            /// </summary>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public bool DeleteFile( string targetPath )
            {
                if(File.Exists( targetPath ))
                {
                    try
                    {
                        File.Delete(targetPath);
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("删除文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("Error , {0}文件不存在!!! ", targetPath);
                    return false;
                }
            }
            /// <summary>
            /// 创建一个文件夹
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public bool CreateFolder(string path)
            {
                if (Directory.Exists(path))
                {
                    Console.WriteLine("文件夹{0}已经存在", path);
                    return true;
                }
                else
                {
                    try
                    {
                        DirectoryInfo dirInfo = Directory.CreateDirectory(path);
                        Console.WriteLine("创建文件夹成功 , 创建时间为{0}", Directory.GetCreationTime(path));
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("创建文件夹失败 , 失败原因{0}", e.ToString());
                        return false;
                    }
                }
            }
            /// <summary>
            /// 删除文件夹
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public bool DeleteFolder(string path)
            {
                if (Directory.Exists(path))
                {
                    try
                    {
                        Directory.Delete(path);
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("删除文件夹失败 , 失败原因{0}", e.ToString());
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="oldPath"></param>
            /// <param name="newPath"></param>
            /// <returns></returns>
            public bool MoveFolder(string oldPath , string newPath)
            {
                if (Directory.Exists(oldPath))
                {
                    try
                    {
                        Directory.Move(oldPath, newPath);
                        return true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("移动文件夹{0},失败 , 原因 : {1} ", oldPath, e.ToString());
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("Error , {0}文件夹不存在!!! ", oldPath);
                    return false;
                }
            }
            /// <summary>
            /// 读取文件( 一个个读 )老是在流以外 , 无法读到正确的值
            /// </summary>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public bool ReadOneByOneTest(string targetPath)
            {
                if (File.Exists(targetPath))
                {
                    FileStream fs = new FileStream(targetPath, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    br.BaseStream.Seek(0, SeekOrigin.Begin); //将指针设到开头
                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        try
                        {
                            Console.WriteLine(br.ReadString());
                        }
                        catch (EndOfStreamException e)
                        {
                            Console.WriteLine("已经到了结尾 {0}", e.ToString());
                        }
                    }
                    br.Close();
                    fs.Close();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            /// <summary>
            /// 读取文本
            /// </summary>
            /// <param name="targetPath"></param>
            /// <returns></returns>
            public bool ReadCommon(string targetPath)
            {
                if (File.Exists(targetPath))
                {
                    //using (StreamReader sr = File.OpenText(targetPath)) // 读中文将乱码
                    using( StreamReader sr = new StreamReader( targetPath , UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
                    {
                        string readStr;
                        while ((readStr = sr.ReadLine()) != null)
                        {
                            Console.WriteLine(readStr);
                        }
                        sr.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="targetPath"></param>
            /// <param name="content"></param>
            /// <param name="isNendWarp"></param>
            /// <returns></returns>
            public bool WriteCommon(string targetPath , string content , bool isNendWarp )
            {
                if (File.Exists(targetPath))
                {
                    //using (StreamWriter sw = File.AppendText(targetPath)) // 中文乱码
                    using( StreamWriter sw = new StreamWriter( targetPath , true ,UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
                    {
                        if (isNendWarp)
                        {
                            sw.WriteLine(content);
                        }
                        else
                        {
                            sw.Write(content);
                        }
                        sw.Close();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    开车旅行(codevs 1199)
    云serverlinux又一次挂载指定文件夹(非扩充)
    百度之星资格赛 hdu 4826 Labyrinth 动态规划
    Linux下基于源代码方式安装MySQL 5.6
    ACM-最短路之中的一个个人的旅行——hdu2066
    新辰:共享是SEO的思维 用户是SEO的核心
    servlet中生成验证码
    Linux ls 排序
    我想要得那块牌—记烟台大学第一届"ACM讲堂"
    coocs2d-html5在使用cocoseditor时调用设备的accelerometer来使用重力感应
  • 原文地址:https://www.cnblogs.com/Pinapple/p/8392714.html
Copyright © 2011-2022 走看看