zoukankan      html  css  js  c++  java
  • 文件流的操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace Command
    {
        public class FilesOperations
        {
    
            public List<string> GetFilesByExtension(string Extension)
            {
                DirectoryInfo folder = new DirectoryInfo(@"D:wms");
    
                foreach (FileInfo file in folder.GetFiles("*." + Extension))
                {
                    return GetTXT(file);
                }
                return null;
            }
    
            private List<string> GetTXT(FileInfo fileinfo)
            {
                List<string> txt = new List<string>();
                if (System.IO.File.Exists(fileinfo.DirectoryName + "\" + fileinfo.Name.Split('.')[0] + ".ok"))
                {
                    var file = System.IO.File.Open(fileinfo.FullName, FileMode.Open);
    
                    using (var stream = new StreamReader(file))
                    {
                        while (!stream.EndOfStream)
                        {
                            txt.Add(stream.ReadLine());
                        }
                    }
    
    
                    //操作完成把操作的文件包括同名.ok文件删除
                    //将 文件 移动到其他目录
                    System.IO.File.Move(fileinfo.DirectoryName + "\" + fileinfo.Name.Split('.')[0] + ".ok", @"D:BackupWms" + fileinfo.Name.Split('.')[0] + ".ok");
                    fileinfo.MoveTo(@"D:BackupWms" + "\" + fileinfo.Name);
    
    
                    file.Close();
                }
                return txt;
            }
    
    
        }
    
        public class FileWrite
        {
            private string fn;
            public FileWrite(string fileNmae)
            {
                fn = fileNmae;
            }
            public bool Files(string s)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(fn, true, Encoding.UTF8);
                    sw.WriteLine(s);
                    sw.Flush();
                    sw.Close();
                    StreamWriter ok = new StreamWriter(fn.Split('.')[0] + ".ok", true, Encoding.UTF8);
                    ok.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
    
            }
        }
    }
  • 相关阅读:
    Win7安装netbeans 找不到JDK
    MyEclipse10 中设置Jquery提醒,亲测可用
    Property 'submit' of object #<HTMLFormElement> is not a function
    JSP Unable to compile class for JSP
    JSP session过期时间(小记)
    JSP乱码(小记)
    JS 正则表达式基础
    CSS 盒子模型
    Cmder--window开发者神器!!
    encodeURI和encodeURIComponent两者的区别
  • 原文地址:https://www.cnblogs.com/liuchang/p/4347866.html
Copyright © 2011-2022 走看看