zoukankan      html  css  js  c++  java
  • File操作

    对文件进行操作(只操作小文件)

    bool Exists(string path)  判断文件是否存在

    FileStream Create(string path)  创建文件

    void Move(string sourcePath,string destPath)  剪切文件

    void Copy(string sourcePath,string destPath)  复制文件(destPath不能是目录或现有文件)

    if (!File.Exists(@"D:UsersDesktopStudy.txt"))
                {
                    File.Create(@"D:UsersDesktopStudy.txt");
                }
                File.Move(@"D:UsersDesktopStudy.txt", @"D:StudyUp.txt");
                File.Copy(@"D:StudyUp.txt", @"D:UsersDesktopStudyTwo.txt");
                File.Delete(@"D:StudyUp.txt");
                File.Delete(@"D:UsersDesktopStudyTwo.txt");

    string[] ReadAllLines()  逐行读取文本

    //ReadAllLines()默认采用的编码格式是utf-8
    string[] str=File.ReadAllLines(@"D:UsersdesktopStudy.txt",Encoding,Default);

    string ReadAllText()  读取文本

    //ReadAllText()默认采用的编码格式是utf-8
    string str=File.ReadAllText(@"D:UsersdesktopStudy.txt");

    byte[] ReadAllBytes()  读取文本

    byte[] buffer=File.ReadAllBytes(@"D:UsersdesktopStudy.txt");
    //字节数组--->字符串
    string str = Encoding.UTF-8.GetString(buffer);
    //如果转成gbk
    string strGBK = Encoding.GetEncoding("gbk").GetString(buffer);

    void WriteAllBytes()  以字节写入文本

    string num="一二三四五六七八九十";
    //字符串--->字节数组
    byte[] buffer=Encoding.Default.GetBytes(num);
    File.WriteAllBytes(@"D:UsersdesktopStudy.txt",buffer);

    void WriteAllLines()   以行的形式写入文本

    File.WriteAllLines(@"D:UsersdesktopStudy.txt",new string[]{"",""});

    void WriteAllText()   以字符串的形式写入文本

    File.WriteAllText(@"D:UsersdesktopStudy.txt","乱七八糟");
  • 相关阅读:
    切割窗口url
    软键盘弹出底部被顶上去
    C语言字符串处理标准库函数的源码(转)
    slapd配置文件详述
    OPENLDAP安装配置方法
    const成员函数
    OPENLDAP概述
    当前比较有名的Xml数据库
    『转』使用 Scalable Vector Graphics 为 ASP.NET 构建基于 XML 的灵活、轻量的图像
    SqlCommand_ExecuteNonQuery 方法返回值为1的解释
  • 原文地址:https://www.cnblogs.com/xiaonangua/p/7299670.html
Copyright © 2011-2022 走看看