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","乱七八糟");
  • 相关阅读:
    android videoView 加载等待
    LocalBroadcastManager
    sessionStorage 、localStorage
    javascript 数组、json连接
    properties 文件注意事项
    nutz 使用beetl
    [Git/Github] ubuntu 14.0 下github 配置
    【UNIX环境编程、操作系统】孤儿进程和僵尸进程
    【操作系统】进程间通信
    【操作系统】线程
  • 原文地址:https://www.cnblogs.com/xiaonangua/p/7299670.html
Copyright © 2011-2022 走看看