C#03 文件IO,读,写,加密

文件IO

抽象类虚方法,继承接口都是为了多态,多态的最终目的是父类控制子类(调用子类方法),多态多用于框架编程

OOP编程:对象,方法
IO命名空间 Using System.IO;
文件: 对于操作系统而言 所有东东都是一种文件, 屏幕 键盘 exe程序 word 等都是文件

咱们今天学习的文件主要是能够使用记事本打开的 程序语言编写的 .cs .c .cpp .java .js .txt .ini .bat.txt

IO:
input: 输入 写文件
output: 输出 读文件

System.IO 包含了针对文件操作的一些class

读文件: StreamReader: 这是一个class 针对能够使用记事本打开的文件类型
构造函数: 构造函数是一种特殊的方法 名字和class相同, 没有返回值类型(非void) 在new的时候被调用, 目的就是开辟内存空间
构造函数: 1. path 2. 编码格式
path: 路径 C:/Demo/Test/1.cs
UTF8
reader.ReadLine(); 读取文本中一行数据 若没有读取到数据就意味着该文本是空/读完了
上述方法的返回值类型为字符串类型

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);
                }
            }
        }

    }
}

写文件 StreamWriter:
构造函数: 1. path
2. 是否追加
3. UTF8
writer.WriteLine(string writeinfo);

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);
            }
        }
    }
}

FileStream 是一个class 针对文件所有类型的文件 这个东东的主要特性是以字节为单位进行操作的

打开 –> 读文件
创建文件/追加 –> 写文件

  1. 计算题 从1.txt文件计算结果写入2.txt
  2. 去掉注释 先读取 string –> 判断(删除,截取) –> 存进ArrayList里面 –> 把ArrayList里面的正常没有注释的写入_copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace 第二题
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入需要去除注释的文件路径");
            string filePath = Console.ReadLine();
            string tmpInfo = string.Empty;
            using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
            {
                string newFilePath = filePath.Insert(filePath.IndexOf("."),"已去注释");
                using (StreamWriter writer = new StreamWriter(newFilePath, true, Encoding.UTF8))
                {
                    while ((tmpInfo = reader.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(tmpInfo))
                        {
                            if (tmpInfo.Trim().Length != 0)
                            {
                                int a = tmpInfo.IndexOf("//");
                                if (a != -1 && !tmpInfo.Contains('"'))
                                {
                                    string str = tmpInfo.Substring(0, a);
                                    if (str.Trim().Length != 0)
                                        writer.WriteLine(str);
                                }
                                else
                                {
                                    writer.WriteLine(tmpInfo);
                                }
                            }
                        }
                    }
                }
            }
                        Console.WriteLine("已去处注释,请按任意键结束");
            Console.ReadLine();
        }
    }
}
  1. 完善加解密 0 加密 1 解密
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("操作完成");
        }
    }
}

改过后

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)
        {
            Console.WriteLine("请输入操作指令,'0'加密,'1'解密");
            string str=Console.ReadLine();
            Console.WriteLine("请输入需要加(解)密的文件路径");
            string filePath = Console.ReadLine();
            byte[] bytes = new byte[filePath.Length];
            FileStream readFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            readFileStream.Read(bytes, 0, bytes.Length);
            readFileStream.Close();
            for (int i = 0; i < bytes.Length; i++)
            {
                if (str=="0")
                {
                    ++bytes[i]; 
                }
                else if (str == "1")
                {
                    --bytes[i];
                }

            }
            //string newpath = filePath.Insert(filePath.IndexOf('.'), "_copy");
            FileStream writeFileSteram = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            writeFileSteram.Write(bytes, 0, bytes.Length);
            writeFileSteram.Close();
            Console.WriteLine("操作完成,请按任意键退出");
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/luxifa1/article/details/82666743