.Net 中操作文本文件
来源:互联网 发布:7层网络协议 http 编辑:程序博客网 时间:2024/05/21 10:52
一 、写入文本
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace PlaceUsingTxt{ public class ClassWriteTxt { public ClassWriteTxt(string url) { TxtUrl = url; fs = new FileStream(TxtUrl, FileMode.Create, FileAccess.ReadWrite); sw = new StreamWriter(fs); } public void close() { sw.Close(); fs.Close(); } protected string TxtUrl; protected FileStream fs; protected StreamWriter sw; public void TxtToBegin() { sw.BaseStream.Seek(0, SeekOrigin.Begin); } public void TxtToEnd() { sw.BaseStream.Seek(0, SeekOrigin.End); } public void WriteLineIntoTxt(string s) { sw.WriteLine(s); sw.Flush(); } public void WriteIntoTxt(string s) { sw.Write(s); sw.Flush(); } }}
二 读文本
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace PlaceUsingTxt{ public class ClassReadTxt { public ClassReadTxt(string url) { TxtUrl = url; fs = new FileStream(TxtUrl, FileMode.Open, FileAccess.ReadWrite); sr = new StreamReader(fs); } public void close() { sr.Close(); fs.Close(); } protected string TxtUrl; protected FileStream fs; protected StreamReader sr; public void TxtToBegin() { sr.BaseStream.Seek(0, SeekOrigin.Begin); } public void TxtToEnd() { sr.BaseStream.Seek(0, SeekOrigin.End); } //逐行读取文本,存于动态数组arraylist中 public System.Collections.ArrayList ReadTxtIotoArray() { System.Collections.ArrayList arr = new System.Collections.ArrayList(); while (sr.Peek() > 0) { arr.Add(sr.ReadLine()); } return arr; } //将array赋给string二维数组,适用于逗号分割的且上下对齐的文本 public string[,] OutStr2D() { this.TxtToBegin(); System.Collections.ArrayList arr = this.ReadTxtIotoArray(); int RowCount = arr.Count; if (RowCount == 0) { return null; } //取得文本列数 string[] strtest = arr[0].ToString().Split(','); int ColumnCount = strtest.Length; string[,] s = new string[RowCount,ColumnCount]; for (int i = 1; i <= RowCount ; i++) { string[] stest = arr[i - 1].ToString().Split(','); for (int j = 1; j <= ColumnCount; j++) { s[i - 1, j - 1] = stest[j - 1]; } } return s; } }}
- .Net 中操作文本文件
- .Net 操作文本文件
- ASP.NET如何操作文本文件
- .Net C# Txt文本文件操作
- 在VB.NET里操作文本文件
- 在VB.NET里操作文本文件
- .net streamReader 和 streamWrite 操作文本文件
- 在ASP.NET页中读取文本文件
- 在ASP.NET页中读取文本文件
- Asp.Net中,怎样生成文本文件
- 在c#中如何操作文本文件
- 操作文本文件
- c# 中读写ini文件(C#操作文本文件、C#读写文本文件)
- ASP.NET中读取文本文件到页面显示
- NET中判断一个文本文件的编码方式
- PHP中对文本文件操作很好的类
- PHP中对文本文件操作很好的类
- PHP中对文本文件操作很好的类
- mysql 用法
- C++ 基础4:数组和指针
- struts2学习笔记之文件上传
- Ubuntu 10.04声卡解决方案
- 034
- .Net 中操作文本文件
- soldering tutorial-http://www.aaroncake.net/electronics/solder.htm
- 第二篇 C
- 半秒内筛一亿以内的所有素数
- 红黑树(一) 插入
- qmake 乱乱乱谈(一)
- 如果你的男朋友是这样 请你离开。女朋友同理 (很经典请牢记)
- 添加菜单遇到的问题引出的linux下gksu和sudo的分别
- Transforms CTM, 转换矩阵