C#文件读写操作实例
来源:互联网 发布:js检验 编辑:程序博客网 时间:2024/05/04 15:06
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Filechange
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\movie\test.txt";
//创建filestream 对象
FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//创建Streamreader进行字节读取
StreamReader sr = new StreamReader(file);
int code = sr.Read();
string str = null;
while (code != -1)
{
str += Convert.ToChar(code);
code = sr.Read();
}
str += " ";
sr.Close();
string str2 = "pc";
char[] chs2 = str.ToCharArray();
for (int i = 0; i < str.Length - 1; i++)
{
if (str.Substring(i, 2) == str2)
{
chs2[i + 2] = 'L';
}
}
string str3 = new string(chs2);
foreach (var item in str3)
Console.Write(item);
//写操作
StreamWriter sw = new StreamWriter(path, false);
sw.Write(str3);
sw.Close();
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Filechange
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\movie\test.txt";
//创建filestream 对象
FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
//创建Streamreader进行字节读取
StreamReader sr = new StreamReader(file);
int code = sr.Read();
string str = null;
while (code != -1)
{
str += Convert.ToChar(code);
code = sr.Read();
}
str += " ";
sr.Close();
string str2 = "pc";
char[] chs2 = str.ToCharArray();
for (int i = 0; i < str.Length - 1; i++)
{
if (str.Substring(i, 2) == str2)
{
chs2[i + 2] = 'L';
}
}
string str3 = new string(chs2);
foreach (var item in str3)
Console.Write(item);
//写操作
StreamWriter sw = new StreamWriter(path, false);
sw.Write(str3);
sw.Close();
Console.ReadKey();
}
}
}
在C#中对文本文件的读写操作还是比较简单的,对于封装好的函数只要稍微注意下使用的方法就应该可以了。
0 0
- C#文件读写操作实例
- C#文件读写IO操作 实例
- C#文件读写实例
- C#文件读写操作
- C#文件读写操作
- c#文件读写操作
- C#文件读写操作
- C# 读写文件操作
- C# 文件读写操作
- c# xml读写 操作 实例
- C# Excel操作读写实例
- C#读写xml文件实例
- C#读写xml文件实例
- C# ini文件读写 实例
- C# ini文件读写 实例
- c#读写xml文件(实例)
- java文件读写操作实例
- PHP 读写文件操作实例
- Unity+NGUI性能优化方法总结
- PRML-系列一之1.2.1
- HTTPS是如何保证连接安全
- 发布不显示jqueryUi控件
- dfs 求全排列
- C#文件读写操作实例
- Introduce to algorithm--------pseudo code to C/C++ code(Chapter 6)
- 面向对象(3)
- css 通用样式头(个人专用)
- JS判断系统
- JS获取URL参数
- 未能从程序集“Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342
- JavaScript 注入攻击
- ①算法之队列解QQ密码