简单记录一次实验室文件操作
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)
{
}
}
}
}
}
}
}
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)
{
}
}
}
}
}
}
}
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- PHP STRING 陷阱原理说明
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享
- asp.net String.IsNullOrEmpty 方法
- JavaScript中字符串(string)转json的2种方法
- C#中string用法实例详解
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码