2个回答
展开全部
byte[] byDataValue = new byte[200];
char[] charDataValue = new char[200];
/// <summary>
/// FilesStream读取用法
/// </summary>
private void FilesStreamReadFile()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.cs", FileMode.Open);
//文件指针移到文件的135个字节
fsFile.Seek(135, SeekOrigin.Begin);
//将接下来的字节读到Array中
fsFile.Read(byDataValue, 0, 200);
}
catch (Exception e)
{
throw e;
}
//将字节转换成字符
Decoder dc = Encoding.UTF8.GetDecoder();
//字节数组转换成字符数组,便於显示
dc.GetChars(byDataValue, 0, byDataValue.Length, charDataValue, 0);
}
/// <summary>
/// FilesStream写入用法
/// </summary>
private void FileStreamWriteFile()
{
FileStream fsFile = new FileStream(@"D:\log.txt", FileMode.Create);
charDataValue="This is test string".ToCharArray();
byDataValue=new byte[charDataValue.Length];
//将字符数组转换成字节数组
Encoder ec=Encoding.UTF8.GetEncoder();
ec.GetBytes(charDataValue,0,charDataValue.Length,byDataValue,0,true);
//将指针设定起始位置
fsFile.Seek(0,SeekOrigin.Begin);
//写入文件
fsFile.Write(byDataValue,0,byDataValue.Length);
}
FileStream对象通常会在StreamWriter或StreamReader内使用,并使用它们的方法处理文件。
/// <summary>
/// StreamWriter写入方法
/// </summary>
private void StreamWriterMetod()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.txt",FileMode.OpenOrCreate);
StreamWriter swWriter = new StreamWriter(fsFile);
//写入数据
swWriter.WriteLine("Hello Wrold.");
swWriter.WriteLine("It is now {0}", DateTime.Now.ToLongDateString());
swWriter.Close();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// StreamWriter读取方法
/// </summary>
private void StreamReaderMetod()
{
string sLine = "";
try
{
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
StreamReader srReader = new StreamReader(fsFile);
//读取文件(读取大文件时,最好不要用此方法)
sLine = srReader.ReadToEnd();
txbValue.Text = sLine;
srReader.Close();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// StreamWriter读取方法2
/// </summary>
private void StreamReaderMetodTwo()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
/*
* 读取数据最简单的方法是Read()。此方法将流的下一个字符作为正整数值返回,
* 如果达到了流的结尾处,则返回-1。
*/
StreamReader srReader = new StreamReader(fsFile);
int iChar;
iChar = srReader.Read();
while (iChar != -1)
{
txbValue.Text += (Convert.ToChar(iChar));
iChar = srReader.Read();
}
srReader.Close();
}
catch (Exception e)
{
throw e;
}
} ");
char[] charDataValue = new char[200];
/// <summary>
/// FilesStream读取用法
/// </summary>
private void FilesStreamReadFile()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.cs", FileMode.Open);
//文件指针移到文件的135个字节
fsFile.Seek(135, SeekOrigin.Begin);
//将接下来的字节读到Array中
fsFile.Read(byDataValue, 0, 200);
}
catch (Exception e)
{
throw e;
}
//将字节转换成字符
Decoder dc = Encoding.UTF8.GetDecoder();
//字节数组转换成字符数组,便於显示
dc.GetChars(byDataValue, 0, byDataValue.Length, charDataValue, 0);
}
/// <summary>
/// FilesStream写入用法
/// </summary>
private void FileStreamWriteFile()
{
FileStream fsFile = new FileStream(@"D:\log.txt", FileMode.Create);
charDataValue="This is test string".ToCharArray();
byDataValue=new byte[charDataValue.Length];
//将字符数组转换成字节数组
Encoder ec=Encoding.UTF8.GetEncoder();
ec.GetBytes(charDataValue,0,charDataValue.Length,byDataValue,0,true);
//将指针设定起始位置
fsFile.Seek(0,SeekOrigin.Begin);
//写入文件
fsFile.Write(byDataValue,0,byDataValue.Length);
}
FileStream对象通常会在StreamWriter或StreamReader内使用,并使用它们的方法处理文件。
/// <summary>
/// StreamWriter写入方法
/// </summary>
private void StreamWriterMetod()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.txt",FileMode.OpenOrCreate);
StreamWriter swWriter = new StreamWriter(fsFile);
//写入数据
swWriter.WriteLine("Hello Wrold.");
swWriter.WriteLine("It is now {0}", DateTime.Now.ToLongDateString());
swWriter.Close();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// StreamWriter读取方法
/// </summary>
private void StreamReaderMetod()
{
string sLine = "";
try
{
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
StreamReader srReader = new StreamReader(fsFile);
//读取文件(读取大文件时,最好不要用此方法)
sLine = srReader.ReadToEnd();
txbValue.Text = sLine;
srReader.Close();
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// StreamWriter读取方法2
/// </summary>
private void StreamReaderMetodTwo()
{
try
{
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
/*
* 读取数据最简单的方法是Read()。此方法将流的下一个字符作为正整数值返回,
* 如果达到了流的结尾处,则返回-1。
*/
StreamReader srReader = new StreamReader(fsFile);
int iChar;
iChar = srReader.Read();
while (iChar != -1)
{
txbValue.Text += (Convert.ToChar(iChar));
iChar = srReader.Read();
}
srReader.Close();
}
catch (Exception e)
{
throw e;
}
} ");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询