国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

c#txt文件的讀寫

2019-11-14 16:13:14
字體:
供稿:網(wǎng)友

namespace file
{
class MyFile
{
string FilePath;
byte[] byData = new byte[100];
public char[] MyData = new char[1000];
public string reslutstr = null;
public MyFile()
{ }
public MyFile(string path)
{
FilePath = path;
}
public void ReadFile1()
{
try
{
FileStream file = new FileStream(FilePath, FileMode.Open);
file.Seek(0, SeekOrigin.Begin);
file.Read(byData, 0, 100); //byData傳進(jìn)來的字節(jié)數(shù)組,用以接受FileStream對象中的數(shù)據(jù),第2個參數(shù)是字節(jié)數(shù)組中開始寫入數(shù)據(jù)的位置,它通常是0,表示從數(shù)組的開端文件中向數(shù)組寫數(shù)據(jù),最后一個參數(shù)規(guī)定從文件讀多少字符.
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(byData, 0, byData.Length, MyData, 0);
//Console.WriteLine(MyData);
foreach (char ch in MyData)
{
reslutstr += ch.ToString();
}
file.Close();
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
}
public void ReadFile2()
{
StreamReader sr = new StreamReader(FilePath, Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
reslutstr += line;
// Console.WriteLine(line.ToString());
}
}
public void SaveFile1(string savestr)
{
FileStream fs = new FileStream(FilePath, FileMode.Create);
//獲得字節(jié)數(shù)組
byte[] data = System.Text.Encoding.Default.GetBytes(savestr);
//開始寫入
fs.Write(data, 0, data.Length);
//清空緩沖區(qū)、關(guān)閉流
fs.Flush();
fs.Close();
}

public void SaveFile2()
{
FileStream fs = new FileStream(FilePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//開始寫入
sw.Write("Hello World!!!!");
//清空緩沖區(qū)
sw.Flush();
//關(guān)閉流
sw.Close();
fs.Close();
}
}
}
//調(diào)用方法:
MyFile MyFile = new MyFile(Filepath);
string result = null;
// MyFile.SaveFile1(savastr);
MyFile.SaveFile2();
MyFile.ReadFile2();


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 介休市| 嫩江县| 神木县| 鹤壁市| 乐陵市| 新巴尔虎左旗| 高雄市| 永平县| 柞水县| 天水市| 湄潭县| 无极县| 邵武市| 祁阳县| 南涧| 镇赉县| 岱山县| 星子县| 开鲁县| 大石桥市| 汝州市| 吉安市| 彩票| 横山县| 海南省| 共和县| 镇平县| 长海县| 龙山县| 阿克苏市| 屏山县| 岢岚县| 莱州市| 洛隆县| 盱眙县| 孟州市| 射阳县| 绿春县| 大田县| 外汇| 浏阳市|