zoukankan      html  css  js  c++  java
  • Path和File

    path类
    string str=@"c:3000soft ed spiderdatamessage老赵.wav";
    Path.GetFileName(str);\获取文件名
    Path.GetFileNameWithoutExtension(str);//获取文件名但是不包括扩展名
    Path.GetExtension(str);//获取文件的扩展名
    Path.GetDirectoryName(str);//获取文件所在文件夹的名称。
    Path.GetFullPath(str);//获取文件所在全路径
    Path.Combine(@"c:a","b.txt");//连接两个字符串作为路径

    File类
    string str=@"c:usersspringRainDesktop ew.txt";
    //创建一个文件
    File.Create(str);
    //删除一个文件
    File.Delete(str);
    //复制一个文件
    File.Copy("原文件fullpath","新文件fullPath");

    读写数据
    File.ReadAllBytes() 字节数组--》字符串 Encoding.Default.GetString(字节数组);
    File.WriteAllBytes()字符串--》字节数组 Encoding.Default.GetBytes(字符串);


    byte[] buffer=File.ReadAllBytes(@"C:users ew.text");
    //将字节数组中的每个元素都要按照我们指定的编码格式解码成字符串
    //UTF-8 GB2312 GBK ASCII Unicode
    string s=Encoding.GetEncoding("GB2312").GetString(buffer);

    //没有这个文件的话,会给你创建一个,有的话会给你覆盖掉
    string str="今天天气好晴朗处处好风光";
    //需要将字符串转换成字节数组
    byte[] buffer=Encoding.Default.GetBytes(str);
    File.WriteAllBytes(@"c:users ew.txt",buffer);

    string[] contents=File.ReadAllLines(@"c: ew.txt",Encoding.Default);
    foreach(string item in contents)
    {
    Console.WriteLine(item);
    }

    string str=File.ReadAllText(@"C: ew.txt",Encoding.Default);
    Console.WriteLine(str);

    1、绝对路径和相对路径
    绝对路径:通过给定的这个路径直接能在我的电脑中找到这个文件。
    相对路径:文件相对于应用程序的路径。
    我们在开发中应该去尽量的使用相对路径。

    File.WriteAllLines(@"C: ew.txt",new string[]{"abc","def"});
    File.WriteAllText(@"c: ew.txt","abcedfg");
    File.AppendAllText(@"c: ew.txt","abcdefg");

  • 相关阅读:
    51nod 1087 1 10 100 1000(找规律+递推+stl)
    51nod 1082 与7无关的数 (打表预处理)
    51 nod 1080 两个数的平方和
    1015 水仙花数(水题)
    51 nod 1003 阶乘后面0的数量
    51nod 1002 数塔取数问题
    51 nod 1001 数组中和等于K的数对
    51 nod 1081 子段求和
    51nod 1134 最长递增子序列 (O(nlogn)算法)
    51nod 1174 区间中最大的数(RMQ)
  • 原文地址:https://www.cnblogs.com/iceberg2008/p/4130961.html
Copyright © 2011-2022 走看看