using System;
using System.IO;
namespace FileClass_Read_and_Wright
{
class Program
{
static void Main(string[] args)
{
/string[] strarray = File.ReadAllLines("TextFile1.txt");//读取文件,把每一行文本文件读取成一个字符串,
//最后主城一个字符串数组
foreach (var s in strarray )
{
Console.WriteLine(s);
}/
/*string s = File.ReadAllText("TextFile1.txt");
Console.WriteLine(s);*/
byte[] byteArray = File.ReadAllBytes("20191103200554.jpg");
foreach (var b in byteArray)
{
Console.Write(b);
}
/*File.WriteAllText("TextFile2.txt", "WEI");
File.WriteAllLines("TextFile3.txt", new string[] { "wei", "zzz" });*/
//读取文件二进制数据
//byte[] data = File.ReadAllBytes("20191103200554.jpg");
//写入文件二进制数据(复制)
//File.WriteAllBytes("1.png", data);
}
}
}
class Program
{
static void Main(string[] args)
{
//Path类与String类相似
string path = @"C:\Users\qweasd\Desktop\NewC\NNN\GaO4\CYG1114\2CYG\1.txt";
/int index = path.LastIndexOf("\");
string s = path.Substring(index + 1);
Console.WriteLine(s);/
//获取文件名
Console.WriteLine( Path.GetFileName(path));
//获取文件名不包含扩展名
Console.WriteLine(Path.GetFileNameWithoutExtension(path));
//获取文件的扩展名
Console.WriteLine(Path.GetExtension(path));
//获取文件所在文件夹的名称
Console.WriteLine(Path.GetDirectoryName(path));
//绝对路径
Console.WriteLine(Path.GetFullPath(path));
//合并路径
Console.WriteLine(Path.Combine(@"c\a","b.txt"));
//File类
//创建文件
/*File.Create("2.txt");
Console.WriteLine("Create f");
Console.ReadKey();*/
//删除文件
//File.Delete(@"D:\715\CSharp\2.txt");
//字符串转字节数组
byte[] bit = Encoding.Default.GetBytes(path);
//字节数组转字符串
string str = Encoding.Default.GetString(bit);
//写入
string ss = "WWWWWWWWWCXRCMCOTXNOMX";
byte[] dt = Encoding.Default.GetBytes(ss);
File.WriteAllBytes("1.txt", dt);
//由字符串数组逐行写入
File.WriteAllLines("1.txt", new string[] { "weqr", "fsdf", "rfsf" });
File.WriteAllText("1.txt", "decwdsq");
//追加写入
File.AppendAllText("1.txt", "---wei");
//读取
byte[] b = File.ReadAllBytes("1.txt");
//相对路径:D:\715\CSharp\文件类\bin\Debug\netcoreapp3.0-程序调试文件夹下
Console.WriteLine(Encoding.UTF8.GetString(b));
File.ReadAllLines(@"D:\715\CSharp\1.txt");//返回字符串数组
File.ReadAllText(@"D:\715\CSharp\1.txt");//返回一个字符串
//FileStream类
//读取数据
/*FileStream fil = new FileStream(@"D:\715\CSharp\1.txt", FileMode.OpenOrCreate, FileAccess.Read);
//参数-FileStream(路径,对文件的操作,对文件里面的数据进行的操作)
byte[] buffer = new byte[1025 * 1024 * 5];
//返回读取到的有效字节数
int r = fil.Read(buffer, 0, buffer.Length);//读取长度
string s = Encoding.Default.GetString(buffer, 0, r);
fil.Close();//关闭流
fil.Dispose();//释放占用资源
Console.WriteLine(s);*/
//写入数据
//将文件流对象写入using{}中,系统会自动释放所占用的资源
/*using (FileStream fileWrite = new FileStream(@"D:\715\CSharp\1.txt", FileMode.Create, FileAccess.Write))
{
string date = "Date Save";
byte[] bufferW = Encoding.Default.GetBytes(date);
fileWrite.Write(bufferW, 0, bufferW.Length);
}*/
string soucre = @"C:\Users\qweasd\Desktop\SAVE.png";
string target = @"D:\715\CSharp\new.png";
CoppyFile(soucre, target);
Console.WriteLine("复制成功");
Console.ReadLine();
//StreamReader & StreamWriter
//文本文件操作
//读取
using (StreamReader sr = new StreamReader(@"D:\715\CSharp\1.txt"))
{
while (!sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
}
}
//写入
using (StreamWriter sw = new StreamWriter (@"D:\715\CSharp\1.txt",true))//bool是否追加文本
{
sw.Write("weiforzz");
}
}
public static void CoppyFile(string soucre,string target)
{
//创建一个读取流
using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
{
//创建一个写入流
using (FileStream fsWrite = new FileStream (target ,FileMode.OpenOrCreate,FileAccess.Write))
{
byte[] buffer = new byte[1024 * 1024 * 5];
while (true)
{
int r = fsRead.Read(buffer, 0, buffer.Length);
if (r == 0)
break;
fsWrite.Write(buffer, 0, r);
}
}
}
}
}
}