using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FileInfo类
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\FileTest\1707A\aa.txt";
string destPath = @"D:\FileTest\1707A\bb.txt";
Test01(path,destPath);
}
static void Test01(string path,string destPath)
{
FileInfo fi = new FileInfo(path);
//获取当前文件所在的目录
Console.WriteLine(fi.Directory);
//获取当前文件所在的目录
Console.WriteLine(fi.DirectoryName);
//判断文件是否是只读的
Console.WriteLine(fi.IsReadOnly);
//获取文件的大小 单位是字节
Console.WriteLine(fi.Length);
//类似拷贝 复制操作 如果目标目录下面有相同的文件 则会抛出异常
//fi.CopyTo(destPath);
//类似剪切操作
//fi.MoveTo(@"D:\FileTest\1707A\YY\bb.txt");
StreamWriter sw = fi.AppendText();
sw.Write("abcdefg");
//强制刷出缓冲区中的数据
sw.Flush();
//关闭流
sw.Close();
//-----------------------------------追加
//-----------------------------------------
FileStream ss = new FileStream(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\FANFAN.txt", FileMode.Create, FileAccess.ReadWrite ,FileShare.ReadWrite);
string str = "67867";
//将字符串转化为字节数组
byte[] bytes = Encoding.UTF8.GetBytes(str);
//文件流写入
ss.Write(bytes, 0, bytes.Length);
//设置文件流位置
ss.Position = 0;
//新建字节组容器
byte[] bytesss = new byte[ss.Length];
//可读的最大字节数
int Max = (int)ss.Length;
//字节的偏移
int offet = 0;
while (Max > 0)
{
//将读到的字节压入容器中
int n = ss.Read(bytesss, offet, Max);
if (n == 0)
break;
Max -= n;
offet += n;
}
Decoder D = Encoding.UTF8.GetDecoder();
Char[] char_date = new char[bytesss.Length];
//将字节数组转化为字符数组 结果: 67867
D.GetChars(bytesss, 0, bytesss.Length, char_date, 0);
Console.WriteLine(char_date);
ss.Flush();
//-------------------------------------------StreamReader
//利用using 不用释放流
FileStream fs = new FileStream(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\笔记.txt", FileMode.Create);
StreamWriter str_w = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
string str_demo = "asdafggg";
str_w.Write(str_demo);
//清空缓存区
str_w.Flush();
//关闭流
str_w.Close();
fs.Close();
using (StreamReader stream_r = new StreamReader(@"C:\Users\GAIVIA-LIZHONGYUAN\Desktop\个人文档\笔记.txt", Encoding.GetEncoding("gb2312")))
{
string lines;
//读取文件中的内容
while ((lines = stream_r.ReadLine()) != null)
{
Console.WriteLine(lines);
}
}
}
}
}
FileInof
最新推荐文章于 2023-08-25 17:17:48 发布