1.Hello,各位码友们大家好,最近遇到了一些问题,大致就是将生成的字符串写入一个项目文件中的一个记事本中,其次就是读取记事本中的内容,并且清空整个记事本中的内容,主要涉及的是,c#中关于文件流的操作,读取和写入文件,接下来,给大家分享一些知识点,有错误的地方还请各位码友们斧正!
2.首先写认识C#中的一个命名空间,操作文件流的时候需要使用它,“System.IO”,它是C#中用于输入和输出操作的命名空间。它提供了许多类和方法,用于处理文件、文件夹和流的读取和写入操作,以下是该命名空间常用的类以及对应的功能。
File类:用于创建、复制、移动、删除和读写文件等操作。它包含一组静态方法,例如Create、Copy、Move、Delete、ReadAllText、WriteAllText等。
Directory类:用于创建、复制、移动和删除文件夹等操作。它包含一组静态方法,例如CreateDirectory、Copy、Move、Delete等。
Path类:用于处理文件和文件夹路径的常见操作。它包含一组静态方法,例如Combine(用于组合路径)、GetExtension(获取文件扩展名)、GetFileName(获取文件名)等。
FileStream类:用于在文件中进行读取和写入操作的流。它允许你以字节为单位读取和写入文件内容。
StreamReader和StreamWriter类:用于以文本方式读取和写入文件内容的流。它们提供了更方便的方法,如逐行读取和写入文本数据。
BinaryReader和BinaryWriter类:用于以二进制格式读取和写入文件内容的流。它们允许你以字节为单位读取和写入数据,适用于处理
3.清除C#项目中文件的内容方法一:
使用File.WriteAllText()
方法,通常用于将一些文本写入文件,要清除文件的内容,您只需使用以下命令将空文本写入文件即可以项目文件中的一个记事本为例子,代码如下:
using System;
using System.IO;
public class Example
{
public static void Main()
{
string path = @"D:\Test\data.txt";
File.WriteAllText(path, string.Empty);
}
}
4.清除C#项目中文件的内容方法二:
使用 FileStream.SetLength()
方法, 创建一个新的文件流,使用FileStream.SetLength()
方法,最后冲洗流。
using System;
using System.IO;
public class Example
{
public static void Main()
{
string path = @"D:\Test\data.txt";
using (FileStream fs = new FileStream(path, FileMode.Open))
{
fs.SetLength(0);
}
}
}
其实,可以直接打开流 FileMode.Truncate
模式,然后关闭它。这将清除文件的内容,甚至不必调用 FileStream.SetLength()
方法。
using System;
using System.IO;
public class Example
{
public static void Main()
{
string path = @"D:\Test\data.txt";
using (FileStream fs = new FileStream(path, FileMode.Truncate)) {
}
}
6.使用 File.Create()
方法
最后,您可以使用 File.Create()
方法,如果文件不存在,则在指定路径创建一个文件。如果该文件确实存在,并且不是只读的,则内容将被覆盖。
using System;
using System.IO;
public class Example
{
public static void Main()
{
string path = @"D:\Test\data.txt";
File.Create(path).Close();
}
}
5. C# 读取和写入文本文件
C#读取文件:
//Read a Text File 读取一个文本文件
using System;
using System.IO;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
String line;
try
{
//将文件路径和文件名传递给StreamReader构造函数
StreamReader sr = new StreamReader(@"D:\Test\data.txt");
//读取文本的第一行
line = sr.ReadLine();
//继续读取,直到到达文件末尾
while (line != null)
{
//将行写入控制台窗口
Console.WriteLine(line);
//读取下一行
line = sr.ReadLine();
}
//关闭文件:将已打开的文件关闭,以便释放系统资源或进行其他操作。
sr.Close();
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
//执行最后一个块
Console.WriteLine("Executing finally block.");
}
}
}
}
C#写入文件:Version1
//Write a text file Version1
using System;
using System.IO;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
//将文件路径和文件名传递给StreamWriter构造函数
StreamWriter sw = new StreamWriter(@"D:\Test\data.txt");
//写第一行文字
sw.WriteLine("Hello World!!");
//写第二行文字
sw.WriteLine("From the StreamWriter class");
//关闭文件
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}
C#写入文件:Version2
//Write a text file - Version2
using System;
using System.IO;
using System.Text;
namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Int64 x;
try
{
//打开文件
StreamWriter sw = new StreamWriter(@"D:\Test\data.txt",true,Encoding.ASCII);
//把数字1到10写在同一行上。
for(x=0; x < 10; x++)
{
sw.Write(x);
}
//关闭文件
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}