using System;
using System.IO;
namespace Class1
{
class Program
{
static void Main(string[] args)
{
//写文件操作
string path = "d:\\test.txt";
string[] data = new string[2];
data[0] = "a";
data[1] = "b";
System.IO.File.WriteAllLines(path, data);
//读文件操作
//1.判断文件是否存在
//2.读
if (System.IO.File.Exists(path))
{
string[] temp = new string[2];
temp = System.IO.File.ReadAllLines(path);
Console.WriteLine(temp[0]);
Console.WriteLine(temp[1]);
}
//删除文件操作
//1.判断文件是否存在
//2.删除
//if (System.IO.File.Exists(path))
//{
// System.IO.File.Delete(path);
//}
//二进制读写操作
//1.创建对应的流
//2.创建对应的二进制读写器
//3.关闭读写器
//4.关闭流
string path1 = "d:\\test1.txt";
string[] data1 = new string[2];
data1[0] = "c";
data1[1] = "d";
//二进制写入
FileStream fs = new FileStream(path1, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(data1[0]);
bw.Write(data1[1]);
bw.Close();
fs.Close();
//二进制读取
if(System.IO.File.Exists(path))
{
fs = new FileStream(path1, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadString());
br.Close();
fs.Close();
}
Console.ReadKey();
}
}
}
C#I/O基本操作(代码)
于 2023-02-14 09:30:12 首次发布