using System;
using System.IO;
namespace _071_File类
{
class Program
{
static void Main(string[] args)
{
//创建一个文件
File.Create(@"C:\Users\46124\Desktop\new.txt");
Console.WriteLine("创建成功!");
//删除一个文件
//File.Delete(@"C:\Users\46124\Desktop\new.txt");
//Console.WriteLine("删除成功");
//复制文件
File.Copy(@"C:\Users\46124\Desktop\新建 Microsoft Word 文档.docx", @"C:\Users\46124\Desktop\new1.docx");
Console.WriteLine("复制成功");
Console.ReadKey();
}
}
}
using System;
using System.IO;
using System.Text;
namespace _073_文件读写
{
class Program
{
static void Main(string[] args)
{
//File.ReadAllBytes()
byte[] buffer = File.ReadAllBytes(@"jueduiyuxiandui.txt");
string s = Encoding.UTF8.GetString(buffer);
Console.WriteLine(s);
Console.WriteLine("====以===上===为===读===取===内===容===");
//File.WriteAllBytes(string path,byte []) 没有指定文件的话会创建有的则覆盖
string s1 = "你是大傻哔";
//需要将字符串转换成字节数组
byte[] buffer1 = Encoding.Default.GetBytes(s1);
File.WriteAllBytes(@"C:\Users\46124\Desktop\test111.txt",buffer1);
Console.WriteLine("写入成功!!!!!!!!!!!!!!");
Console.WriteLine("====以===上===为===读===取===内===容===");
//File.ReadAllLines(string path,Encoding)
string[] contents = File.ReadAllLines("jueduiyuxiandui.txt", Encoding.Default);
foreach (string item in contents)
{
Console.WriteLine(item);
}
Console.WriteLine("====以===上===为===读===取===内===容===");
//File.ReadAllText(string path,Encoding)
string s2 = File.ReadAllText("jueduiyuxiandui.txt", Encoding.Default);
Console.WriteLine(s);
Console.WriteLine("====以===上===为===读===取===内===容===");
//File.WriteAllLines(string path,string[] contents) 没有指定文件的话会创建有的则覆盖
string[] contents1 = { "秀", "天秀", "陈独秀", "蒂花之秀" };
File.WriteAllLines(@"C:\Users\46124\Desktop\text112.txt", contents1);
Console.WriteLine("写入成功!!!!!!!!!!!!!!");
//File.WriteAllText(string path,string) 没有指定文件的话会创建有的则覆盖
string str = "秀\n天秀\n陈独秀\n蒂花之秀\n";
File.WriteAllText(@"C:\Users\46124\Desktop\text113.txt", str);
Console.WriteLine("写入成功!!!!!!!!!!!!!!");
//File.AppendAllText(string path,string) 追加文字不会覆盖
File.AppendAllText(@"C:\Users\46124\Desktop\text113.txt", "我是追加的文字");
Console.WriteLine("追加成功!!!!!!!!!!!!!!");
Console.ReadKey();
}
}
}