您的位置:首页 > 其它

简单记录一次实验室文件操作

2016-04-27 14:44 337 查看
给自己看,也就自己能看懂...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var file = File.Open("D:\\a.txt", FileMode.Open);
int countNum = 0;
List<string> txt = new List<string>();
using (var stream = new StreamReader(file))
{
while (!stream.EndOfStream)
{
txt.Add(stream.ReadLine());
}
Console.WriteLine(txt.Count);
int temp = 0;
int index = 0;
FileStream fcreat,fwrite;
for (int i = 0; i < txt.Count; i++)
{
if (txt[i] == "tms")
{
countNum = 0;
if (index < 10)
{
fcreat = new FileStream("D:\\path\\" + "0" + index + ".txt", FileMode.Create, FileAccess.Write);
fcreat.Flush();
fcreat.Close();
}
else
{
fcreat = new FileStream("D:\\path\\" + index + ".txt", FileMode.Create, FileAccess.Write);
fcreat.Flush();
fcreat.Close();
}
index++;
temp = index - 1;
continue;
}
else
{
try
{
if (index <= 10)
{
fwrite = new FileStream("D:\\path\\" + "0" + temp + ".txt", FileMode.Append, FileAccess.Write);
}
else
{
fwrite = new FileStream("D:\\path\\" + temp + ".txt", FileMode.Append, FileAccess.Write);
}
if (txt[i].Length <= 1) continue;
if (txt[i] == null) continue;
countNum++;
txt[i] = txt[i].Replace(" ", " ");
string[] tempStr = txt[i].Split(' ');
//if (tempStr[3] == "e" || tempStr[5] == "e")
//{
// tempStr[3] = "1000";
// tempStr[5] = "1000";
//}
//byte[] str1 = System.Text.Encoding.Default.GetBytes(tempStr[3]);
//byte[] str2 = System.Text.Encoding.Default.GetBytes(tempStr[5]);

byte[] str2 = System.Text.Encoding.Default.GetBytes(tempStr[1]);

string nextine = "\r\n";
string s = " ";
byte[] empty = System.Text.Encoding.Default.GetBytes(s);
byte[] nextlines = System.Text.Encoding.Default.GetBytes(nextine);

if (countNum == 250)
{
//rite.Write(str1, 0, str1.Length);
//rite.Write(empty, 0, s.Length);
//rite.Write(str2, 0, str2.Length);
fwrite.Write(str1, 0, str2.Length);
fwrite.Flush();
fwrite.Close();
countNum = 0;
}
else
{
//rite.Write(str1, 0, str1.Length);
//rite.Write(empty, 0, s.Length);
fwrite.Write(str2, 0, str2.Length);
fwrite.Write(nextlines, 0, nextlines.Length);
fwrite.Flush();
fwrite.Close();
}
}
catch (Exception ex)
{

}
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string