using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 读取文件
{
class Program
{
staticvoid 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
{
staticvoid 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
{
staticvoid Main(string[] args)
{
//定义一个路径string filePath = Console.ReadLine();
Console.WriteLine("请耐心等待程序完成");
//读文件固定格式
FileStream readFileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
//定义一个字节数组 长度就是改文件的总长度byte[] bytes = newbyte[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("操作完成");
}
}
}