網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
提供用于創(chuàng)建、復(fù)制、刪除、移動(dòng)和打開單一文件的靜態(tài)方法,并協(xié)助創(chuàng)建?FileStream?對(duì)象。
一、讀文件:
1、返回字符串:
string readText = File.ReadAllText(@"c:\temp\MyTest.txt");
2、返回字符串?dāng)?shù)組:
string[] readText = File.ReadAllLines(@"c:\temp\MyTest.txt", Encoding.UTF8);
3、返回字節(jié)數(shù)組:
byte[] buffer = File.ReadAllBytes(@"c:\temp\MyTest.txt");
string str = Encoding.Default.GetString(buffer, 0, buffer.Length);
4、返回StreamReader
打開現(xiàn)有的UTF-8文本以進(jìn)行讀取
using (StreamReader sr = File.OpenText(@"c:\temp\MyTest.txt"))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
二、寫文件
創(chuàng)建一個(gè)新文件向其中寫入內(nèi)容,文件已存在則覆蓋。
1、寫入字符串:
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);//File.WriteAllText(),F(xiàn)ile.AppendAllText()
2、寫入字符串?dāng)?shù)組:
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
3、寫入字節(jié)數(shù)組:
string str = "哈哈哈哈哈哈";
byte[] buffer = Encoding.Default.GetBytes(str);
File.WriteAllBytes(path,buffer);
4、返回StreamWriter
創(chuàng)建或打開現(xiàn)有的UTF-8文本,以進(jìn)行寫入或追加
using (StreamWriter sw = File.CreateText(path)) //StreamWriter:File.CreateText()、File.AppendText()
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
三、返回FileStream的操作
File.Open():默認(rèn)為不共享、具有讀/寫訪問權(quán)限
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, 0, b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
File.OpenRead():讀訪問權(quán)限
略
File.OpenWrite:寫訪問權(quán)限
using (FileStream fs = File.OpenWrite(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
File.Create():
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
四、File類的常用操作:
- 文件刪除方法:File.Delete()
- 文件復(fù)制方法:File.Copy()
- 文件移動(dòng)方法:File.Move()
- 設(shè)置文件屬性方法:File.Set/Get***()
- 判斷文件是否存在的方法:File.Exist()
五、Directory類的常用操作:
//刪除此目錄
Directory.Delete(@"C:\新建文件夾")
//刪除此目錄,true表示要是此目錄有子目錄也刪除,否則就拋出異常
Directory.Delete(@"C:\新建文件夾", false);
//此目錄是否存在
bool b = Directory.Exists(@"C:\新建文件夾");
//根據(jù)路徑返回此目錄下的子目錄
string[] dirs1 = Directory.GetDirectories(@"C:\新建文件夾");
//第二個(gè)參數(shù)表示:搜索的范圍,就是搜索的文件夾包含“基礎(chǔ)”關(guān)鍵字
string[] dirs2 = Directory.GetDirectories(@"C:\新建文件夾", "基礎(chǔ)");
//搜索目錄下的所有文件
string[] files = Directory.GetFiles(@"C:\新建文件夾");
//第三個(gè)參數(shù)表示:指定搜索操作應(yīng)包括所有子目錄還是僅包括當(dāng)前目錄。
string[] files1 = Directory.GetFiles(@"C:\新建文件夾", "2.txt", SearchOption.AllDirectories);
//(獲取指定目錄下的所有目錄時(shí)返回一個(gè)DirectoryInfo數(shù)組。)
DirectoryInfo dirs = Directory.GetParent(@"C:\新建文件夾");
//移動(dòng)、剪切。只能在同一個(gè)磁盤中。目錄沒有Copy方法。可以使用Move()方法實(shí)現(xiàn)重命名。
Directory.Move(@"F:\測(cè)試\33", @"F:\測(cè)試\32\33");
六、FileSystemInfo
派生類:
- DirectoryInfo
- FileInfo
1、FileInfo類
//實(shí)例化FileInfo進(jìn)行操作
FileInfo myfile = new FileInfo(path); //聲明一個(gè)對(duì)象對(duì)某一個(gè)文件進(jìn)行操作
myfile.CopyTo(destpath); //對(duì)文件進(jìn)行復(fù)制操作,復(fù)制路徑為destpath
myfile.MoveTo(destpath); //進(jìn)行移動(dòng)操作
myfile.Delete(); //進(jìn)行刪除操作
//獲得某一文件或文件夾的詳細(xì)信息(創(chuàng)建日期,最后一次修改日期等等)
FileInfo myfile = new FileInfo(path); //聲明一個(gè)對(duì)象對(duì)某一個(gè)文件進(jìn)行操作
DateTime dt = myfile.CreationTime; //獲取或設(shè)置文件/文件夾的創(chuàng)建日期
string filepath = myfile.DirectoryName; //僅能用于FileInfo,獲得完整的路徑名,路徑+文件名
bool file = myfile.Exists; //此屬性的值表示文件或文件夾是否存在,存在會(huì)返回True
string fullname = myfile.FullName; //獲取文件或文件夾的完整路徑名
DateTime lastTime = myfile.LastAccessTime; //獲取或設(shè)置最后一次訪問文件或文件夾的時(shí)間
DateTime lastWrite = myfile.LastWriteTime; //獲取或設(shè)置最后一次修改文件夾或文件夾的時(shí)間
string name = myfile.Name; //獲取文件名,不能修改哦
long length = myfile.Length; //返回文件的字節(jié)大小
//CreationTime,LastAccessTime,LastWriteTime都是可以被修改的。
2、DirectoryInfo類
DirectoryInfo dir = new DirectoryInfo(@"d:\C#程序設(shè)計(jì)");
if (!dir.Exists)
{
dir.Create();
}
else
{
Console.WriteLine("該目錄已經(jīng)存在");
}
七、DriveInfo類
在Windows操作系統(tǒng)中,存儲(chǔ)介質(zhì)統(tǒng)稱為驅(qū)動(dòng)器,硬盤由于可以劃分為多個(gè)區(qū)域,每一個(gè)區(qū)域稱為一個(gè)驅(qū)動(dòng)器。
DriveInfo類的常用字段成員有
- DriveFormat(文件系統(tǒng)格式,如NTFS或FAT32)、
- DriveType(驅(qū)動(dòng)器類型)、
- Name(驅(qū)動(dòng)器名)、
- TotalSize(總空間)、
- TotalFreeSpace(獲得驅(qū)動(dòng)器可用空間)。
常用的方法成員有GetDrives(獲得可用驅(qū)動(dòng)器列表)。
DriveType枚舉型的枚舉值有CDRom(光驅(qū))、Fixed(硬盤)、Network(網(wǎng)絡(luò)驅(qū)動(dòng)器)和Removeable(軟盤或U盤)等。
例如,以下代碼可以輸出每一個(gè)硬盤驅(qū)動(dòng)器的剩余空間信息。
DriveInfo[] drivers = DriveInfo.GetDrives();
foreach (DriveInfo driver in drivers)
{
if (driver.DriveType == DriveType.Fixed && driver.DriveFormat == "NTFS")
{
Console.WriteLine("在{0}驅(qū)動(dòng)器上還有{1}字節(jié)的剩余空間。", driver.Name, driver.AvailableFreeSpace);
}
}
原文鏈接:https://www.cnblogs.com/springsnow/p/9428702.html
相關(guān)推薦
- 2023-10-14 uni-app adb安卓wifi無(wú)線調(diào)試
- 2023-02-12 一文帶你了解Golang中reflect反射的常見錯(cuò)誤_Golang
- 2022-09-16 C語(yǔ)言庫(kù)函數(shù)getchar()新見解_C 語(yǔ)言
- 2022-11-02 使用ggsignif優(yōu)雅添加顯著性標(biāo)記詳解_R語(yǔ)言
- 2022-06-01 Android利用MediaRecorder實(shí)現(xiàn)錄音功能_Android
- 2022-07-15 SQL?Server分頁(yè)方法匯總_MsSql
- 2022-08-29 教你nginx跳轉(zhuǎn)配置的四種方式_nginx
- 2022-05-26 mongoDB數(shù)據(jù)庫(kù)索引快速入門指南_MongoDB
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支