说明:这是以前在CSDN上写的一篇BLOG,现在搬过来^_^
大家都知道,对文件的操作少了要用到FileStream类/StreamWriter类/StreamReader这三个类中一个或是几个,下面就通过一个简单的例子,来对这三个类的使用做一个介绍吧。。
说明:这是以前在CSDN上写的一篇BLOG,现在搬过来^_^
大家都知道,对文件的操作少了要用到FileStream类/StreamWriter类/StreamReader这三个类中一个或是几个,下面就通过一个简单的例子,来对这三个类的使用做一个介绍吧。
Code
/// <summary>
/// 文件夹类[Directory]/文件类[File]
/// FileStream类/StreamWriter类/StreamReader的使用
/// </summary>
class Method
{
/// <summary>
/// 创建文件夹的方法
/// </summary>
public string creatFold(string foldPath)
{
//创建文件夹
Directory.CreateDirectory(foldPath);
Console.WriteLine("创建文件夹成功了");
Console.ReadLine();
return foldPath;
}
/// <summary>
/// 创建文件方法
/// </summary>
//public void creatFile(string filePath)
//{
// //创建文件
// File.Create(filePath);
// Console.WriteLine("创建文件成功");
// Console.ReadLine();
// return;
//}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="isfile"></param>
/// <returns></returns>
public bool isFile(bool isfile, string filePath)
{
//以路径为参数构建FileInfo对象,用于检测文件相关信息
FileInfo myFileInfo = new FileInfo(filePath);
if (myFileInfo.Exists)
{
isfile = true;
}
else
{
isfile = false;
}
return isfile;
}
/// <summary>
/// 检查文件夹是否存在
/// </summary>
/// <param name="isfold"></param>
/// <returns></returns>
public bool isFold(bool isfold,string foldPath)
{
//以路径为参数构建DirectoryInfo对象,用于检测文件夹相关信息
DirectoryInfo myDirectoryInfo = new DirectoryInfo(foldPath);
if (myDirectoryInfo.Exists)
{
isfold = true;
}
else
{
isfold = false;
}
return isfold;
}
/// <summary>
/// 向文件中写入内容
/// </summary>
/// <param name="fs"></param>
/// <param name="value"></param>
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
/// <summary>
/// 向文件中写入内容
/// </summary>
public void WriteFileInfo(string path)
{
try
{
//创建FileStream对象
FileStream myWriteFileStr = File.Open(path, FileMode.OpenOrCreate);
AddText(myWriteFileStr, "OH,My God!");
AddText(myWriteFileStr, "\n我竟然被写入了文件中!");
AddText(myWriteFileStr, "\n这太神奇了!\n");
myWriteFileStr.Close();
}
catch (IOException ex)
{
Console.WriteLine("写入时出错啦!");
Console.WriteLine(ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("写入文件成功了!一起来庆祝一下吧!");
Console.ReadLine();
}
/// <summary>
/// 向文件中添加新的内容
/// </summary>
/// <param name="path"></param>
public void AddAgainText(string path)
{
try
{
//在原有文件中添加内容
StreamWriter myStrWrite = new StreamWriter(path, true);
myStrWrite.WriteLine("Hello,我新来的,请大家多多关照!");
myStrWrite.Close();
}
catch (IOException ex)
{
Console.WriteLine("写入时出错啦!");
Console.WriteLine(ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("向文件中添加新的内容成功了!我太兴奋了!");
Console.ReadLine();
}
/// <summary>
/// 读出文件中的内容方法1
/// </summary>
/// <param name="filePath"></param>
public void ReadFileInfo(string filePath)
{
try
{
FileStream myReadFileStr = File.OpenRead(filePath);
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
Console.WriteLine("\n文件中的内容为[方法1]:");
while (myReadFileStr.Read(b, 0, b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
myReadFileStr.Close();
}
catch (IOException ex)
{
Console.WriteLine("读出文件内容时出错了!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Console.ReadLine();
}
/// <summary>
/// 读出文件中的内容方法2
/// </summary>
/// <param name="filePath"></param>
public void ReadStreamFile(string filePath)
{
try
{
StreamReader mySR = new StreamReader(filePath);
Console.WriteLine("\n文件中的内容为[方法2]:");
Console.WriteLine(mySR.ReadToEnd());
mySR.Close();
}
catch(IOException ex)
{
Console.WriteLine("读出文件内容时出错了!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Console.ReadLine();
}
/// <summary>
/// 输出文件相关信息
/// </summary>
/// <param name="fileFoldInfos"></param>
public void FileFoldInfos(string fileFoldInfos)
{
//以路径为参数构建FileInfo对象,用于检测文件相关信息
FileInfo myFileInfo = new FileInfo(fileFoldInfos);
//输出文件相关信息
Console.WriteLine("文件的相关信息如下:");
Console.WriteLine("文件的父目录:" + myFileInfo.Directory);
Console.WriteLine("文件创建的日期:" + myFileInfo.CreationTime);
Console.WriteLine("文件所在目录的完整路径:" + myFileInfo.DirectoryName);
Console.WriteLine("文件的完整目录:" + myFileInfo.FullName);
Console.WriteLine("文件的扩展名:" + myFileInfo.Extension);
Console.WriteLine("文件大小:" + myFileInfo.Length);
Console.WriteLine("文件名:" + myFileInfo.Name);
}
}
/// <summary>
/// 文件夹及文件路径类
/// </summary>
class FileFoldDir
{
/// <summary>
/// 文件夹要创建的绝对路径字段
/// </summary>
private string myFoldPath =string.Empty;
/// <summary>
/// 文件夹要创建的绝对路径属性
/// </summary>
public string MyFoldPath
{
get
{
return myFoldPath;
}
set
{
myFoldPath = value;
}
}
/// <summary>
/// 文件创建的绝对路径字段
/// </summary>
private string myFilePath =string.Empty;
/// <summary>
/// 文件夹要创建的绝对路径属性
/// </summary>
public string MyFilePath
{
get
{
return myFilePath;
}
set
{
myFilePath = value;
}
}
}
第二、写个主类运行一下吧:/// <summary>
/// 文件夹类[Directory]/文件类[File]
/// FileStream类/StreamWriter类/StreamReader的使用
/// </summary>
class Method
{
/// <summary>
/// 创建文件夹的方法
/// </summary>
public string creatFold(string foldPath)
{
//创建文件夹
Directory.CreateDirectory(foldPath);
Console.WriteLine("创建文件夹成功了");
Console.ReadLine();
return foldPath;
}
/// <summary>
/// 创建文件方法
/// </summary>
//public void creatFile(string filePath)
//{
// //创建文件
// File.Create(filePath);
// Console.WriteLine("创建文件成功");
// Console.ReadLine();
// return;
//}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="isfile"></param>
/// <returns></returns>
public bool isFile(bool isfile, string filePath)
{
//以路径为参数构建FileInfo对象,用于检测文件相关信息
FileInfo myFileInfo = new FileInfo(filePath);
if (myFileInfo.Exists)
{
isfile = true;
}
else
{
isfile = false;
}
return isfile;
}
/// <summary>
/// 检查文件夹是否存在
/// </summary>
/// <param name="isfold"></param>
/// <returns></returns>
public bool isFold(bool isfold,string foldPath)
{
//以路径为参数构建DirectoryInfo对象,用于检测文件夹相关信息
DirectoryInfo myDirectoryInfo = new DirectoryInfo(foldPath);
if (myDirectoryInfo.Exists)
{
isfold = true;
}
else
{
isfold = false;
}
return isfold;
}
/// <summary>
/// 向文件中写入内容
/// </summary>
/// <param name="fs"></param>
/// <param name="value"></param>
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
/// <summary>
/// 向文件中写入内容
/// </summary>
public void WriteFileInfo(string path)
{
try
{
//创建FileStream对象
FileStream myWriteFileStr = File.Open(path, FileMode.OpenOrCreate);
AddText(myWriteFileStr, "OH,My God!");
AddText(myWriteFileStr, "\n我竟然被写入了文件中!");
AddText(myWriteFileStr, "\n这太神奇了!\n");
myWriteFileStr.Close();
}
catch (IOException ex)
{
Console.WriteLine("写入时出错啦!");
Console.WriteLine(ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("写入文件成功了!一起来庆祝一下吧!");
Console.ReadLine();
}
/// <summary>
/// 向文件中添加新的内容
/// </summary>
/// <param name="path"></param>
public void AddAgainText(string path)
{
try
{
//在原有文件中添加内容
StreamWriter myStrWrite = new StreamWriter(path, true);
myStrWrite.WriteLine("Hello,我新来的,请大家多多关照!");
myStrWrite.Close();
}
catch (IOException ex)
{
Console.WriteLine("写入时出错啦!");
Console.WriteLine(ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("向文件中添加新的内容成功了!我太兴奋了!");
Console.ReadLine();
}
/// <summary>
/// 读出文件中的内容方法1
/// </summary>
/// <param name="filePath"></param>
public void ReadFileInfo(string filePath)
{
try
{
FileStream myReadFileStr = File.OpenRead(filePath);
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
Console.WriteLine("\n文件中的内容为[方法1]:");
while (myReadFileStr.Read(b, 0, b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
myReadFileStr.Close();
}
catch (IOException ex)
{
Console.WriteLine("读出文件内容时出错了!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Console.ReadLine();
}
/// <summary>
/// 读出文件中的内容方法2
/// </summary>
/// <param name="filePath"></param>
public void ReadStreamFile(string filePath)
{
try
{
StreamReader mySR = new StreamReader(filePath);
Console.WriteLine("\n文件中的内容为[方法2]:");
Console.WriteLine(mySR.ReadToEnd());
mySR.Close();
}
catch(IOException ex)
{
Console.WriteLine("读出文件内容时出错了!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Console.ReadLine();
}
/// <summary>
/// 输出文件相关信息
/// </summary>
/// <param name="fileFoldInfos"></param>
public void FileFoldInfos(string fileFoldInfos)
{
//以路径为参数构建FileInfo对象,用于检测文件相关信息
FileInfo myFileInfo = new FileInfo(fileFoldInfos);
//输出文件相关信息
Console.WriteLine("文件的相关信息如下:");
Console.WriteLine("文件的父目录:" + myFileInfo.Directory);
Console.WriteLine("文件创建的日期:" + myFileInfo.CreationTime);
Console.WriteLine("文件所在目录的完整路径:" + myFileInfo.DirectoryName);
Console.WriteLine("文件的完整目录:" + myFileInfo.FullName);
Console.WriteLine("文件的扩展名:" + myFileInfo.Extension);
Console.WriteLine("文件大小:" + myFileInfo.Length);
Console.WriteLine("文件名:" + myFileInfo.Name);
}
}
/// <summary>
/// 文件夹及文件路径类
/// </summary>
class FileFoldDir
{
/// <summary>
/// 文件夹要创建的绝对路径字段
/// </summary>
private string myFoldPath =string.Empty;
/// <summary>
/// 文件夹要创建的绝对路径属性
/// </summary>
public string MyFoldPath
{
get
{
return myFoldPath;
}
set
{
myFoldPath = value;
}
}
/// <summary>
/// 文件创建的绝对路径字段
/// </summary>
private string myFilePath =string.Empty;
/// <summary>
/// 文件夹要创建的绝对路径属性
/// </summary>
public string MyFilePath
{
get
{
return myFilePath;
}
set
{
myFilePath = value;
}
}
}
Code
class Program
{
static void Main(string[] args)
{
//定义isExist,用于判断文件夹或文件是否存在
bool isExist = false;
//定义一个FileFoldDir对象,用于设置文件夹及文件的绝对路径
FileFoldDir myFileFoldDir = new FileFoldDir();
//设置文件夹及文件的绝对路径
myFileFoldDir.MyFoldPath = @"E:\files";
myFileFoldDir.MyFilePath = @"E:\files\myfile.txt";
//实例化一个Method对象
Method myMethod = new Method();
if (myMethod.isFile(isExist, myFileFoldDir.MyFilePath))
{
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
else
{
//判断文件夹是否存在
if (myMethod.isFold(isExist, myFileFoldDir.MyFoldPath))
{
//如文件存在,则写入内容;否则创建文件,再写入内容
myMethod.WriteFileInfo(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
else
{
//创建文件夹
myMethod.creatFold(myFileFoldDir.MyFoldPath);
//创建文件,并写入内容
myMethod.WriteFileInfo(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
}
}
}
class Program
{
static void Main(string[] args)
{
//定义isExist,用于判断文件夹或文件是否存在
bool isExist = false;
//定义一个FileFoldDir对象,用于设置文件夹及文件的绝对路径
FileFoldDir myFileFoldDir = new FileFoldDir();
//设置文件夹及文件的绝对路径
myFileFoldDir.MyFoldPath = @"E:\files";
myFileFoldDir.MyFilePath = @"E:\files\myfile.txt";
//实例化一个Method对象
Method myMethod = new Method();
if (myMethod.isFile(isExist, myFileFoldDir.MyFilePath))
{
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
else
{
//判断文件夹是否存在
if (myMethod.isFold(isExist, myFileFoldDir.MyFoldPath))
{
//如文件存在,则写入内容;否则创建文件,再写入内容
myMethod.WriteFileInfo(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
else
{
//创建文件夹
myMethod.creatFold(myFileFoldDir.MyFoldPath);
//创建文件,并写入内容
myMethod.WriteFileInfo(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法1]
myMethod.ReadFileInfo(myFileFoldDir.MyFilePath);
//再加入新的内容
myMethod.AddAgainText(myFileFoldDir.MyFilePath);
//输出文件的相关信息
myMethod.FileFoldInfos(myFileFoldDir.MyFilePath);
//读出文件内容[方法2]
myMethod.ReadStreamFile(myFileFoldDir.MyFilePath);
}
}
}
}
最后别忘记了引用以下命名空间:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
Demo下载:/Files/asidy/ConsoleApplication2.rar