.Netでの操作テキストファイル
一、テキストを書き込む
によみとりテキスト
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;
}
}
}