zoukankan      html  css  js  c++  java
  • MVC07

    1. 讲解ASP.net MVC的I/O操作

    新建一个控制台程序,输入代码如下

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 判断文件是否存在
                Console.WriteLine(File.Exists(@"C:UsersASUSDesktopmemoa.txt"));
                // 判断目录是否存在
                Console.WriteLine(Directory.Exists(@"C:"));
                // 下面例子将展示如何查找某一目录下所有exe文件的信息
                // . 表示当前目录
                string path = ".";
                if (args.Length > 0)
                {
                    // 如果需要在其他目录执行则打开控制行,之后进入项目下的Debug目录,见下图
                    path = args[0];
                }
                else
                {
                    Console.WriteLine("Directory not found");
                }
    
                DirectoryInfo dir = new DirectoryInfo(path);
                foreach(FileInfo f in dir.GetFiles("*.exe"))
                {
                    string name = f.Name;
                    long size = f.Length;
                    DateTime creationTime = f.CreationTime;
                    Console.WriteLine(name);
                    Console.WriteLine(size);
                    Console.WriteLine(creationTime);
                    Console.WriteLine("------------");
                }
            }
        }
    }

    File,Directory为一个静态的Class,无法实例化。

    2.写入文件

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private const string FILE_NAME = "a.txt";
            static void Main(string[] args)
            {
                if (File.Exists(FILE_NAME))
                {
                    Console.WriteLine("already exists.");
                    return;
                }
    
                FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
                BinaryWriter w = new BinaryWriter(fs);
    
                for(int i = 0; i < 10; i++)
                {
                    w.Write("a");
                }
                w.Close();
                fs.Close();
            }
        }
    }

     

    如果文件已存在,我们需要覆盖内容到里面怎么办?

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private const string FILE_NAME = "a.txt";
            static void Main(string[] args)
            {
                using(StreamWriter w = File.AppendText("test.txt"))
                {
                    Log("Hi,is me.", w);
                    Log("how are u", w);
                    w.Close();
                }
               
            }
            // 方法,用于写入数据
            public static void Log(string logMessage,TextWriter w)
            {
                w.Write("
    Log Entry");
                w.WriteLine(":{0}", logMessage);
                w.Flush();
    
            }
        }
    }

    using 内代码执行完毕后会自动释放资源,常用于读写文件以及连接数据库

    2.读取文件

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private const string FILE_NAME = "a.txt";
            static void Main(string[] args)
            {
                if (!File.Exists(FILE_NAME))
                {
                    Console.WriteLine("{0} does not exist!",FILE_NAME);
                    return;
                }
                // 路径,操作类别,权限
                FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
           // 读取前5个字符
    for(int i = 0; i < 5; i++) { Console.WriteLine(r.ReadString()); } r.Close(); fs.Close(); } } }

    完整读取某一文件:

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private const string FILE_NAME = "test.txt";
            static void Main(string[] args)
            {
                if (!File.Exists(FILE_NAME))
                {
                    Console.WriteLine("{0} does not exist!",FILE_NAME);
                    return;
                }
                using(StreamReader sr = File.OpenText(FILE_NAME))
                {
                    string input;
                    while((input = sr.ReadLine())!=null)
                    {
                        Console.WriteLine(input);
                    }
                    Console.WriteLine("ended");
                    sr.Close();
                }
            }
    
        }
    }
  • 相关阅读:
    Highmaps网页图表教程之图表配置项结构与商业授权
    Highmaps网页图表教程之Highmaps第一个实例与图表构成
    Highmaps网页图表教程之下载Highmaps与Highmaps的地图类型
    iOS 9应用开发教程之使用开关滑块控件以及滚动部署视图
    iOS 9应用开发教程之多行读写文本ios9文本视图
    基于STM32的电池管理系统触摸屏设计方案
    如何使用UDP进行跨网段广播(转)
    STM32的NVIC_PriorityGroupConfig使用及优先级分组方式理解
    基于MDK编程STM32程序无法使用,硬件仿真在汇编窗口看到停留在“0x0800XXXX BEAB BKPT 0xAB //进入调试模式”
    IIR 滤波器的实现(C++)
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12252858.html
Copyright © 2011-2022 走看看