读取文件,写文件,文件加密

读取文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  System.IO;

namespace 读取文件
{
    class Program
    {
        static void Main(string[] args)
        {
            //在进行文件读写的时候, 我们使用using这个东东 可以帮助我们关闭文件 比较便利 固定格式

            string filePath = @"D:\LLWH\work\day03\笔记\笔记.txt"; // 定义路径
            string tmpInfo = string.Empty; // 临时读取的内容
            using (StreamReader reader = new StreamReader(filePath,Encoding.UTF8))
            {
                //固定格式
                while ((tmpInfo = reader.ReadLine()) != null)
                {
                    //读取的一行数据
                    Console.WriteLine(tmpInfo);
                }
            }
        }

    }
}

写文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  System.IO;

namespace 写文件
{
    class Program
    {
        static void Main(string[] args)
        {

            string filePath = @"D:\LLWH\work\day03\笔记\Write.txt"; // 定义路径
            //固定格式
            using (StreamWriter writer = new StreamWriter(filePath,false,Encoding.UTF8))
            {
                Console.WriteLine("请输入相对我说的话......");
                //接受用户输入
                string userInput = Console.ReadLine();
                //将字符串写入对应的文本里面去
                writer.WriteLine(userInput);
            }
        }
    }
}

文件加密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using  System.IO;

namespace 加密
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义一个路径
            string filePath = Console.ReadLine();
            Console.WriteLine("请耐心等待程序完成");
            //读文件固定格式
            FileStream readFileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
            //定义一个字节数组 长度就是改文件的总长度
            byte[] bytes = new byte[readFileStream.Length];

            //存储的字节数组 偏移量 最多读取多少个
            readFileStream.Read(bytes, 0, bytes.Length);
            //关闭该文件
            readFileStream.Close();

            //加密过程
            for (int i = 0; i < bytes.Length; i++)
            {
                ++bytes[i];
            }

//弄一个新的文件
            string newPath = filePath.Insert(filePath.IndexOf('.'), "_copy");
            //写文件的固定格式
            FileStream writeFileStream = new FileStream(newPath,FileMode.Create,FileAccess.Write);
            //将加密之后的数据写入新的文件
            writeFileStream.Write(bytes,0,bytes.Length);
            //关闭
            writeFileStream.Close();
            Console.WriteLine("操作完成");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值