美文网首页
c#文件简单读取和写入

c#文件简单读取和写入

作者: orange_6217 | 来源:发表于2018-07-24 16:46 被阅读0次

using System;

using System.IO;

using System.Text;

namespace HelloWord

{

    class Program

    {

        static void Main(string[] args)

        {

            //获取某目录下文件大小、名称、创建时间

            /*Boolean file_flag = File.Exists(@"D:\createuser\createuser.sql");          //"@"代表后面输入的都是字符

            Boolean folder_flag = Directory.Exists(@"D:\createuser");

            String path = ".";

            if (args.Length > 0)

            {

                if (Directory.Exists(args[0]))

                {

                    path = args[0];

                }

                else

                {

                    Console.WriteLine("{0} not found;using current directory;", args[0]);

                }

            }

            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("{0, -12:N0}{1, -20:g}{2}", size, creationTime, name);

            }

            Console.WriteLine(path);

            //Console.WriteLine(file_flag + "--" + folder_flag);

            Console.ReadLine();*/

            /*byte[] byData = new byte[100];

            char[] charData = new char[1000];

            FileStream fs = new FileStream(@"D:\createuser\createuser.sql", FileMode.Open);

            fs.Seek(0,SeekOrigin.Begin);

            fs.Read(byData, 0, 100);

            Decoder d = Encoding.Default.GetDecoder();

            d.GetChars(byData,0,byData.Length,charData,0);

            Console.WriteLine(charData);

            Console.ReadLine();*/

            string str = Program.Read(@"D:\createuser\createuser1.sql");

            Boolean flag = Program.Write(str);

            Console.WriteLine(flag);

            Console.ReadLine();

        }

        public static string Read(string path)

        {

            StreamReader sr = new StreamReader(path, Encoding.Default);

            String str = "";

            String line;

            while ((line = sr.ReadLine()) != null)

            {

                str = str + line.ToString()+"\n";

            }

            return str;

        }

        public static Boolean Write(string str)

        {

            FileStream fs = new FileStream(@"D:\createuser\createuser2.sql", FileMode.Create);

            StreamWriter sw = new StreamWriter(fs);

            sw.Write(str);

            sw.Flush();

            sw.Close();

            fs.Close();

            return true;

        }

    }

}

相关文章

  • fs文件系统操作

    基础写入文件 简单写入文件 流式文件写入 简单文件读取 流式文件读取 流式文件拷贝(读取 + 写入) 复制文件 f...

  • c#文件简单读取和写入

    using System; using System.IO; using System.Text; namespa...

  • Python读取写入获取文件简单例子

    Python读取写入获取文件简单例子

  • 【Nodejs笔记】nodejs入门简介_文件读取和写入

    这篇简单的整理了nodejs的buffer, 文件读取和写入, 以及流方式读取和写入。 欢迎大家的意见和交流 em...

  • python 文件操作

    fp=open("文件路径","方式") 文件读取 文件写入 文件关闭 文件读取写入方式

  • c#读取、写入 xml文件

    付一张xml文件 1.创建xml文件 设置xml文档的标头 XmlDocument xmldoc = new Xm...

  • C#大文件读取写入

    接上一篇文章,继续大文件的问题 1.这次我还是创建控制台程序,按自己需求自定义数组的大小读取 2.创建一个控制程序...

  • 文件操作

    读取和写入:文件有内容读取就是将文件中的内容读取到内存中。写入就是将内存中的内容写入到磁盘文件中。内存就相当于你的...

  • txt读写

    文件打开 读文件 读取字符串 按行读取整个文件 写文件 字符串写入txt 列表写入文件 双层列表写入文件 数组写入文件

  • nodeJS读取json文件并写入txt或redis中

    【1:读取json写入txt文件】json文件book.json js文件 【2:读取json写入redis文件/...

网友评论

      本文标题:c#文件简单读取和写入

      本文链接:https://www.haomeiwen.com/subject/txrymftx.html