ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【C#学习记录】六、文件读写相关操作

2022-02-28 19:00:47  阅读:205  来源: 互联网

标签:文件 记录 C# 读写 System FileStream File new using


目录

Path类

在System.IO命名空间中,是一个静态类

常用方法

  1. Path.GetFileName();

    获得文件名

  2. Path.GetFileNameWithoutExtension();

    获得文件名但不包含扩展名

  3. Path.GetExtension();

    获得文件的扩展名

  4. Path.GetDirectoryName();

    获得文件所在的文件夹的名称

  5. Path.GetFullPath();

    获得文件的全路径

  6. Path.Combined();

    连接两个字符串作为路径

File类

File类只能读取小文件,因为只能一次性读写,大文件需要用FileStream类。

文件操作函数

  1. File.Create();

    创建一个文件,如果有这个文件,则不会创建新的文件,但文件修改时间被修改。

  2. File.Delete();

    删除一个文件

  3. File.Copy();

    复制一个文件

简单的文件读写

编码:将字符串以怎样的形式保存为二进制

乱码:产生乱码的原因:就是你保存这个文件所采用的编码,跟你打开这个文件所采用的编码格式不一样。

编码特点 特点
Asc 128
ASCII 256
GB2312 包含简体字
Big5 包含繁体字
unicode 比较全,解析起来慢
UTF-8 针对web的编码
  1. File.ReadAllBytes()

    字符数组-->字符串,Encoding.Default.GetString(字节数组)

  2. File.WriteAllByte()

    字符串-->字符数组,Encoding.Default.GetByte(字符串),没有这个文件的话,会给你创建一个,有的话会覆盖掉。

读文件方法 说明
File.ReadAllByte() 可以读任何文件,按照字节读取,但是需要Encoding.Default.GetString()转换成字符串,或转换成其他格式
File.ReadAllLine() 只能读取TXT文件,按照行读取,输出string[]
File.ReadAllText() 只能读取TXT文件,读取全部文件,输出string[]
File.WriteAllLines() 按照行写TXT文件,File.WriteAllLines(path, new string[]{"str1","str2"});
File.WriteAllText() 写所有TXT文件,File.WriteAllText(path, str);
File.AppendAllText() File.AppendAllText(path,str),不会覆盖掉原文件

相对路径和绝对路径

绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。

相对路径:文件相对于应用程序的路径。

我们在开发中应该去尽量的使用相对路径。

FileStream类

类别 特点
File 一次性读写
FileStream 按流读写,操作字节的
StreamReader/StreamWriter 按流读写,操作字符的

使用FileStream读文件

using System.IO;
using System.Text;
string path = @"C:\Users\徐志强\Desktop";
FileStream fsRead = new FileStream(path + @"\testFile.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[1024 * 1024 * 5];
//r存储的是这次读出来的实际有效字节数
int r = fsRead.Read(buffer,0,buffer.Length);
//将字节数组中每一个元素按照指定的编码格式解码成字符串
string s = Encoding.Default.GetString(buffer,0,r);
//关闭流
fsRead.Close();
//释放流所占用的资源
fsRead.Dispose();
Console.WriteLine(s);
Console.ReadKey();

将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源

using System.IO;
using System.Text;
string path = @"C:\Users\徐志强\Desktop";

using (FileStream fsWrite = new FileStream(path+@"\testFile.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
    string str = "看这段文字有没有覆盖掉原文";
    byte[] buffer2 = Encoding.Default.GetBytes(str);
    fsWrite.Write(buffer2);
}
Console.Clear();
using (FileStream fsRead = new FileStream(path+@"\testFile.txt",FileMode.OpenOrCreate,FileAccess.Read))
{
    byte[] buffer3 = new byte[1024*1024*5];
    int r = fsRead.Read(buffer3,0,buffer3.Length);
    string s = Encoding.Default.GetString(buffer3,0,r);
    Console.WriteLine(s);
}
Console.ReadKey();

多媒体文件的赋值

错误的做法:

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

namespace FileStream类
{
    internal class CopyFile
    {
        public static void CopyFileFcn(string source, string target)
        {
            using (FileStream fsRead = new FileStream(source,FileMode.OpenOrCreate,FileAccess.Read))
            {
                byte[] bufferRead = new byte[1024 * 1024 * 5];
                int r = fsRead.Read(bufferRead, 0, bufferRead.Length);
                while (r != 0)
                {
                    using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        fsWrite.Write(bufferRead, 0, r);
                    }
                    r = fsRead.Read(bufferRead, 0, bufferRead.Length);
                }
            }
        }
    }
}

正确的做法:

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

namespace FileStream类
{
    internal class CopyFile
    {
        public static void CopyFileFcn(string source, string target)
        {
            using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] bufferRead = new byte[1024 * 1024 * 5];
                using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    int r = fsRead.Read(bufferRead, 0, bufferRead.Length);
                    while (r != 0)
                    {
                        fsWrite.Write(bufferRead, 0, r);
                        r = fsRead.Read(bufferRead, 0, bufferRead.Length);
                    }
                }
            }
        }
    }
}

请注意这里的while用法!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

当一个文件比较大需要循环读取的时候,不能循环创建文件流。

文件流循环读取过程中,会自动记录当前读取的byte位置,读取的offset设置成0即可。

当读取返回0,也就意味着什么都没有读到,读取完了。

StreamWriter/StreamReader类

using System.IO;

string strSourceTxt = @"C:\Users\徐志强\Desktop\testFile.txt";

using (StreamWriter sw = new StreamWriter(strSourceTxt,true,Encoding.Default))
{
    sw.Write("啦啦啦啦啦啦啦啦啦啦啦");
}
using (StreamReader sr = new StreamReader(strSourceTxt, Encoding.Default))
{
    while (!sr.EndOfStream)
    {
        Console.WriteLine(sr.ReadLine());
    }
}

标签:文件,记录,C#,读写,System,FileStream,File,new,using
来源: https://www.cnblogs.com/xuzhq/p/15947004.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有