image.png

    1. using System;
    2. using System.IO;
    3. namespace _071_File
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. //创建一个文件
    10. File.Create(@"C:\Users\46124\Desktop\new.txt");
    11. Console.WriteLine("创建成功!");
    12. //删除一个文件
    13. //File.Delete(@"C:\Users\46124\Desktop\new.txt");
    14. //Console.WriteLine("删除成功");
    15. //复制文件
    16. File.Copy(@"C:\Users\46124\Desktop\新建 Microsoft Word 文档.docx", @"C:\Users\46124\Desktop\new1.docx");
    17. Console.WriteLine("复制成功");
    18. Console.ReadKey();
    19. }
    20. }
    21. }

    image.png
    image.png

    image.png

    1. using System;
    2. using System.IO;
    3. using System.Text;
    4. namespace _073_文件读写
    5. {
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. //File.ReadAllBytes()
    11. byte[] buffer = File.ReadAllBytes(@"jueduiyuxiandui.txt");
    12. string s = Encoding.UTF8.GetString(buffer);
    13. Console.WriteLine(s);
    14. Console.WriteLine("====以===上===为===读===取===内===容===");
    15. //File.WriteAllBytes(string path,byte []) 没有指定文件的话会创建有的则覆盖
    16. string s1 = "你是大傻哔";
    17. //需要将字符串转换成字节数组
    18. byte[] buffer1 = Encoding.Default.GetBytes(s1);
    19. File.WriteAllBytes(@"C:\Users\46124\Desktop\test111.txt",buffer1);
    20. Console.WriteLine("写入成功!!!!!!!!!!!!!!");
    21. Console.WriteLine("====以===上===为===读===取===内===容===");
    22. //File.ReadAllLines(string path,Encoding)
    23. string[] contents = File.ReadAllLines("jueduiyuxiandui.txt", Encoding.Default);
    24. foreach (string item in contents)
    25. {
    26. Console.WriteLine(item);
    27. }
    28. Console.WriteLine("====以===上===为===读===取===内===容===");
    29. //File.ReadAllText(string path,Encoding)
    30. string s2 = File.ReadAllText("jueduiyuxiandui.txt", Encoding.Default);
    31. Console.WriteLine(s);
    32. Console.WriteLine("====以===上===为===读===取===内===容===");
    33. //File.WriteAllLines(string path,string[] contents) 没有指定文件的话会创建有的则覆盖
    34. string[] contents1 = { "秀", "天秀", "陈独秀", "蒂花之秀" };
    35. File.WriteAllLines(@"C:\Users\46124\Desktop\text112.txt", contents1);
    36. Console.WriteLine("写入成功!!!!!!!!!!!!!!");
    37. //File.WriteAllText(string path,string) 没有指定文件的话会创建有的则覆盖
    38. string str = "秀\n天秀\n陈独秀\n蒂花之秀\n";
    39. File.WriteAllText(@"C:\Users\46124\Desktop\text113.txt", str);
    40. Console.WriteLine("写入成功!!!!!!!!!!!!!!");
    41. //File.AppendAllText(string path,string) 追加文字不会覆盖
    42. File.AppendAllText(@"C:\Users\46124\Desktop\text113.txt", "我是追加的文字");
    43. Console.WriteLine("追加成功!!!!!!!!!!!!!!");
    44. Console.ReadKey();
    45. }
    46. }
    47. }