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","乱七八糟");
  • 相关阅读:
    jmeter 工具学习 未完待续
    测试学习
    JavaScript
    新概念第1册
    day08
    LeetCode OJ:Invert Binary Tree(反转二叉树)
    LeetCode OJ:Lowest Common Ancestor of a Binary Search Tree(最浅的公共祖先)
    LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)
    LeetCode OJ:Path Sum(路径之和)
    LeetCode OJ:Symmetric Tree(对称的树)
  • 原文地址:https://www.cnblogs.com/xiaonangua/p/7299670.html
Copyright © 2011-2022 走看看