博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件读取、写入
阅读量:4453 次
发布时间:2019-06-07

本文共 1542 字,大约阅读时间需要 5 分钟。

   

文件的读操作

   static void Main(string[] args)

        {
            string path = "";
            Console.WriteLine("请输入要读取的文件的文件名,包括路径");
            path = Console.ReadLine();
            if (!File.Exists(path))
            {
                Console.WriteLine("文件不存在");
                return;
            }

            try

            {
                FileStream file = new FileStream(path, FileMode.Open);
                byte[] bt = new byte[file.Length];
                file.Read(bt, 0, bt.Length);
                string str = Encoding.Default.GetString(bt);
                Console.WriteLine(str);
                Console.ReadLine();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("读取文件出错");
            }

        }

 

文件的写操作:

       static void Main(string[] args)

        {
            //FileStream fs1 = File.Create("test1");
            //fs1.Close();
            //Console.ReadLine();

            string path = "";

            string content = "";
            Console.WriteLine("请输入要保存的文件的文件名,包括路径");
            path = Console.ReadLine();
            Console.WriteLine("请输入要保存的内容 ");
            content = Console.ReadLine();
            try
            {
                FileStream file = new FileStream(path, FileMode.Create);
                byte[] bt = Encoding.UTF8.GetBytes(content);
                file.Write(bt, 0, bt.Length);
                file.Flush();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("创建或写入文件时出错");
             
            }

 

 // 读取文件流

     static void Main(string[] args)

        {
            Console.WriteLine("请输入要读取文件的文件名,包括路径");
            string path = Console.ReadLine();
            if (!File.Exists(path))
            {
                Console.WriteLine("文件不存在");
                return;
            }

            FileStream readStream = new FileStream(path, FileMode.Open);

            BufferedStream readBuffered = new BufferedStream(readStream);
            byte[] bt = new byte[readBuffered.Length];
            readBuffered.Read(bt, 0, (int)readBuffered.Length);
            Console.WriteLine(Encoding.Default.GetString(bt));
            readBuffered.Close();
            Console.ReadLine();
        }

 

转载于:https://www.cnblogs.com/tianjinquan/archive/2010/10/22/1858425.html

你可能感兴趣的文章
算法:求从1到n这n个整数的十进制表示中1出现的次数-- python 实现
查看>>
CSU 1160 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示
查看>>
LintCode 58: Compare Strings
查看>>
[Unity插件]Lua行为树(五):装饰节点Repeater
查看>>
顺序表、链表、栈和队列
查看>>
Linux第二天(Linux常用命令2)
查看>>
MySql知识体系
查看>>
JIRA中的标记语言的语法参考
查看>>
hdu 6318 Swaps and Inversions(归并排序)
查看>>
用css在IE7、8上实现圆角
查看>>
三维绿幕标定与跟踪
查看>>
android ProgressBar自定义半圆形进度条
查看>>
局部变量的值赋给成员变量 案例(红色字体)
查看>>
Django
查看>>
hdu.5212.Code(莫比乌斯反演 && 埃氏筛)
查看>>
python学习记录一
查看>>
使用LINQ的Skip和Take函数分批获取数据
查看>>
IP通信基础 4月1日
查看>>
KeyProvider
查看>>
空指针为什么能调用成员函数?
查看>>