FileStream

FileStream读取数据

//FileStream读取数据
FileStream fsRead = new FileStream(@"D:\123.txt", FileMode.OpenOrCreate, FileAccess.Read);//路径;打开或创建;读
byte[] buffer = new byte[1024 * 1024 * 5];//每次都5兆
//返回实际读到的字节数
int r = fsRead.Read(buffer, 0, buffer.Length);//参数:数组;读取起始位置;读取长度
//将自己数组中的每一个元素按照指定编码格式解码成字符串
string s = Encoding.Default.GetString(buffer,0,r);//0到r解码
//关闭流
fsRead.Close();
//释放流所占的资源
fsRead.Dispose();
//打印
Console.WriteLine(s);
Console.ReadKey();

FileStream写入数据

using (FileStream fsWrite = new FileStream(@"D:\123.txt", FileMode.OpenOrCreate, FileAccess.Read)) {
                string str = "覆盖";
                byte[] buffer = Encoding.Default.GetBytes(str);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            Console.WriteLine("写入ok");
            Console.ReadKey();

文件流写入

public static byte[] fileWrite(byte[] buffer) {
string path = Application.StartupPath + "\\Picture\\123.txt";// debug\\picture\\123.txt
FileStream fs = new FileStream(path, FileMode.Append);
//转为string
string temp = StringHelper.ByteArray2String(buffer);
temp += " ";
byte[] array = Encoding.UTF8.GetBytes(temp);
fs.Write(array, 0, array.Length);
fs.Close();
return buffer;
//MessageBox.Show("写入完成");

}
/// <summary>
        /// 向txt文件写入内容
        /// </summary>
        /// <param name="path">txt文件保存的路径,没有就创建,有内容就覆盖。例:"E:\\text.txt"</param>
        /// <param name="contentSrt">要写入的内容</param>
        private void WriteForTxt(string path,string contentSrt)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter wr = null;
            wr = new StreamWriter(fs);
            wr.WriteLine(contentSrt);
            wr.Close();
        }

 

文件流读取

public static byte[] FileRead(byte[] data) {
string path = Application.StartupPath + "\\Picture\\123.txt";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[5242880 * 4 * 24];//每次都5兆
int r = fs.Read(buffer, 0, buffer.Length);
string s = Encoding.Default.GetString(buffer, 0, r);//0到r解码

List<byte> videoDataList = new List<byte>();
var videodData = StringHelper.String2ByteArray(s, 16);
videoDataList.AddRange(videodData);
data = videoDataList.ToArray();

fs.Close();
fs.Dispose();
return data;
//MessageBox.Show("读取完成");
}

  

posted @ 2022-01-02 18:06  驼七  阅读(64)  评论(0)    收藏  举报