zoukankan      html  css  js  c++  java
  • 25.File类

    方法

    Create():创建文件

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                //创建一个文件
                File.Create(@"C:Users22053Desktop
    ew.txt");
                Console.WriteLine("创建成功");
                Console.ReadKey();
            }
        }
    
    }
    

    Delete():删除文件

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                //删除一个文件
                File.Delete(@"C:Users22053Desktop
    ew.txt");
                Console.WriteLine("删除成功");
                Console.ReadKey();
            }
        }
    
    }
    

    Copy():复制文件

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
    
                //复制一个文件
                File.Copy(@"C:Users22053Desktop
    ew.txt", @"C:Users22053Desktopa.txt");
                Console.WriteLine("复制成功");
                Console.ReadKey();
            }
        }
    
    }
    

    Move():剪切文件

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                File.Move(@"C:Users22053Desktop
    ew.txt", @"C:Users22053Desktopa.txt");
                Console.WriteLine("剪切成功");
                Console.ReadKey();
            }
        }
    
    }
    

    WriteAllBytes():以字节的形式向文件中写入数据

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
               
    
                string s = "今天天气好晴朗,处处好风光";
                //将字符串转换成字节数组
                byte[] buffer = Encoding.Default.GetBytes(s);
                //以字节的形式向计算机中写入文本文件
                File.WriteAllBytes(@"C:Users22053Desktop
    ew.txt", buffer);
                Console.WriteLine("写入成功");
                Console.ReadKey();
            }
        }
    
    }
    

    ReadAllBytes():从文件中读取数据

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                //从文件中读取数据
                byte[] buffer = File.ReadAllBytes(@"C:Users22053Desktop
    ew.txt");
    
                //写入
                File.WriteAllBytes(@"C:Users22053Desktopa.txt", buffer);
                
                Console.ReadKey();
            }
        }
    
    }
    

    Encoding

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
    
                byte[] buffer = File.ReadAllBytes(@"C:Users22053Desktop
    ew.txt");
                //将字节数组中的每一个元素都要按照我们指定的编码格式解码成字符串
                //UTF-8  GB2312 GBK ASCII  Unicode
                //string s = Encoding.UTF8.GetString(buffer);
                string s = Encoding.Default.GetString(buffer);
    
                Console.WriteLine(s);
                Console.ReadKey();
    
            }
        }
    
    }
    

    ReadAllLines():将文件的所有行读入一个字符串数组

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                string[] contents = File.ReadAllLines(@"C:Users22053Desktop
    ew.txt", Encoding.Default);
                foreach (string item in contents) {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
    
            }
        }
    
    }
    

    绝对路径和相对路径

    绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。
    相对路径:文件相对于应用程序的路径。

    ReadAllText():将文件中的所有文本读取到一个字符串中

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                //此处使用的是相对路径
                string str = File.ReadAllText("new.txt", Encoding.Default);
                Console.WriteLine(str);
                Console.ReadKey();
    
            }
        }
    
    }
    

    WriteAllLines():创建一个新文件,在其中写入一个或多个字符串

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                File.WriteAllLines(@"C:Users22053Desktop
    ew.txt", new string[] { "aoe", "ewu" });
                Console.WriteLine("OK");
                Console.ReadKey();
    
    
            }
        }
    
    }
    

    WriteAllText():创建一个新文件,向其中写入内容

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                File.WriteAllText(@"C:Users22053Desktop
    ew.txt", "张三李四王五赵六");
                Console.WriteLine("OK");
                Console.ReadKey();
    
            }
        }
    
    }
    

    AppendAllText():将指定的字符串追加到文件中

    namespace Demo {
     
        class Program {
    
            static void Main(string[] args) {
    
                File.AppendAllText(@"C:Users22053Desktop
    ew.txt", "看我有木有把你覆盖掉");
                Console.WriteLine("OK");
                Console.ReadKey();
            }
        }
    
    }
    

    更多方法请参考官方文档

  • 相关阅读:
    JavaScript借助xpath操纵xml数据(一)
    JavaScript借助xpath操纵xml数据(二)
    保留域——$$Return
    ExtJS中get、getDom、getCmp、getBody、getDoc使用 javascript
    关于编辑器编码保存为utf8问题
    query 用法
    简历模版(抛砖引玉)总觉得自己简历做得不够好的朋友请进来
    sql 查询慢的48个原因分析
    Word 实用技巧整理
    JAVA文件操作大全
  • 原文地址:https://www.cnblogs.com/lz32158/p/12919098.html
Copyright © 2011-2022 走看看