目錄
一、簡介
二、創(chuàng)建文件
三、寫入文件
四、讀取文件
五、復(fù)制文件
六、移動文件
七、重命名文件
八、刪除文件
結(jié)束
一、簡介
C#中的IO(Input/Output)操作包括讀取和寫入文件、讀取和寫入流、以及操作目錄和文件夾等。這些操作都可以通過System.IO命名空間中的類實現(xiàn)。下面對C# IO相關(guān)的類和操作進(jìn)行介紹。
文件操作
File類
File類提供了對文件的創(chuàng)建、讀取、寫入、復(fù)制、移動、重命名和刪除等操作。
StreamReader和StreamWriter類
StreamReader和StreamWriter類用于讀取和寫入文本文件。
目錄和文件夾操作
Directory和DirectoryInfo類
Directory和DirectoryInfo類提供了對目錄和文件夾的創(chuàng)建、移動、重命名和刪除等操作。
二、創(chuàng)建文件
要創(chuàng)建一個文件,可以使用System.IO.File.Create()方法。該方法接受一個文件路徑和一個可選的緩沖區(qū)大小作為參數(shù),然后返回一個FileStream對象,該對象可用于寫入文件。
代碼:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
try
{
string filePath = @"E:\myFile.txt";
FileStream fileStream = File.Create(filePath);
fileStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("創(chuàng)建文件失?。? + ex.Message);
}
Console.WriteLine("創(chuàng)建文件成功!");
Console.ReadKey();
}
}
}
打開E盤,文件是創(chuàng)建成功了,只是這個 txt 文件里沒有任何內(nèi)容
三、寫入文件
寫入文件可以使用下面方式:
1.?File.WriteAllText(FilePath,String)
2.?File.WriteAllText(FilePath, String,Encoding) ----指定編碼
3.?File.WriteAllLines(FilePath,String[])
4.?File.WriteAllLines(FilePath,String[],Encoding) ----指定編碼
前面兩種寫入的是一個字符串,后面兩種寫入的是一個字符串?dāng)?shù)組。
使用 File.WriteAllText 或 File.WriteAllLines 方法時,如果指定的文件路徑不存在,會創(chuàng)建一個新文件;如果文件已經(jīng)存在,則會覆蓋原文件。
第一種:字符串的寫入
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string content = "讀取讀取讀取讀取讀取讀取";
//第一種
//File.WriteAllText(filePath, content);
//第二種
File.WriteAllText(filePath, content, Encoding.UTF8);
Console.ReadKey();
}
}
}
打開剛創(chuàng)建的 myFile.txt 文件:
第二種:多行字符串的寫入
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string[] contentArr = { "dddd", "eeeeee" };
//第一種
//File.WriteAllLines(filePath, contentArr);
//第二種
File.WriteAllLines(filePath, contentArr, Encoding.UTF8);
Console.ReadKey();
}
}
}
上面寫入的字符串現(xiàn)在已經(jīng)被覆蓋了
上面寫入字符串都會覆蓋原有的內(nèi)容,下面就介紹如何追加字符串。
第三種:追加字符串
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string content = "423423423423";
File.AppendAllText(filePath, content);
Console.ReadKey();
}
}
}
filePath 路徑這個文件如果沒有,C# 會自動生成這個文件,運行后找到這個文件,就可以看到添加的內(nèi)容
下面我將內(nèi)容做一下更改,再次運行
namespace Test1
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string content = "dffsddgaadfasdfa";
File.AppendAllText(filePath, content);
Console.ReadKey();
}
}
}
我們重寫打開?myFile.txt 這個文件,發(fā)現(xiàn)文字是追加上去了,但是并沒有換行
其實換行也非常的簡單,直接在字符串里加轉(zhuǎn)義字符 \n 就行了
namespace Test1
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string content = "\n哇哈哈哈哈";
File.AppendAllText(filePath, content);
Console.ReadKey();
}
}
}
效果:
第四種:追加多行字符串
按上面的套路,添加多行當(dāng)然也是可以的,不過用的方法名要換一個了
使用?File.AppendAllLines 來添加多行文字。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string[] list = { "\n恭喜", "發(fā)財", "紅包", "拿來" };
File.AppendAllLines(filePath, list, Encoding.UTF8);
Console.ReadKey();
}
}
}
運行后:
四、讀取文件
讀取文件可以使用下面方式:
1.File.ReadAllText(FilePath)
2.File.ReadAllText(FilePath, Encoding) ----指定編碼
3.File.ReadAllLines(FilePath)
4.File.ReadAllLines(FilePath, Encoding) ----指定編碼
以字符串接收方式
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string path = @"E:\myFile.txt";
//string content = File.ReadAllText(path);
string content = File.ReadAllText(path,Encoding.UTF8);
Console.WriteLine(content);
Console.ReadKey();
}
}
}
以字符串?dāng)?shù)組接收的方式
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string path = @"E:\myFile.txt";
//string[] content = File.ReadAllLines(path);
string[] content = File.ReadAllLines(path, Encoding.UTF8);
for (int i = 0; i < content.Length; i++)
{
Console.WriteLine(content[i]);
}
Console.ReadKey();
}
}
}
采用流(Stream)的方式來讀取內(nèi)容
1.StreamReader(FilePath)
2.StreamReader(FilePath, Encoding)
3.StreamReader(FileStream)
4.StreamReader(FileStream, Encoding)
5.File.OpenText(FilePath)
6.FileInfo.OpenText()
基于StreamReader,一行一行讀取
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string path = @"E:\myFile.txt";
//StreamReader sr1 = new StreamReader(path);
//StreamReader sr2 = new StreamReader(path, Encoding.UTF8);
//初始化FileStream
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
//StreamReader sr3 = new StreamReader(fs);
StreamReader sr4 = new StreamReader(fs, Encoding.UTF8);
string line = string.Empty;
while((line = sr4.ReadLine()) != null)
{
Console.WriteLine(line);
}
sr4.Close();
Console.ReadKey();
}
}
}
一次性讀完
string path = @"E:\myFile.txt";
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string content = sr.ReadToEnd();
Console.WriteLine(content);
sr.Close();
五、復(fù)制文件
要復(fù)制文件,可以使用System.IO.File.Copy()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件復(fù)制到目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string sourceFilePath = @"E:\myFile.txt";
string destinationFilePath = @"E:\myFile_copy.txt";
File.Copy(sourceFilePath, destinationFilePath);
Console.ReadKey();
}
}
}
打開對應(yīng)的目錄,可以看到拷貝成功了,而且字節(jié)數(shù)也是一樣的
六、移動文件
要移動文件,可以使用System.IO.File.Move()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件移動到目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string sourceFilePath = @"E:\myFile.txt";
string destinationFilePath = @"D:\myFile.txt";
File.Move(sourceFilePath, destinationFilePath);
Console.ReadKey();
}
}
}
七、重命名文件
要重命名文件,可以使用System.IO.File.Move()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件重命名為目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。
在上一節(jié) 移動文件 中,我們將 myFile.txt 文件移動到 D 盤了,執(zhí)行下面操作之前,要先剪切回來再執(zhí)行操作哦
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string filePath = @"E:\myFile.txt";
string newFilePath = @"E:\myFile_new.txt";
File.Move(filePath, newFilePath);
Console.ReadKey();
}
}
}
運行后,可以看到效果了
?
八、刪除文件
要刪除文件,可以使用System.IO.File.Delete()方法。該方法接受文件路徑作為參數(shù),并將該文件從磁盤上刪除。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 文件操作
{
internal class Program
{
static void Main(string[] args)
{
string newFilePath = @"E:\myFile_new.txt";
File.Delete(newFilePath);
Console.ReadKey();
}
}
}
結(jié)束
如果這個帖子對你有所幫助,歡迎 關(guān)注 + 點贊 + 留言文章來源:http://www.zghlxwxcb.cn/news/detail-429892.html
end文章來源地址http://www.zghlxwxcb.cn/news/detail-429892.html
到了這里,關(guān)于C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!