处理txt文件的数据
我想实现如下功能,点击一个转换按钮将打开的一个文本文件的数据进行处理。原文件是
姓名 年龄 性别
1, 2, 女
3,4, 男
处理后显示
1 女
3 男
没有思路,不知道该如何下手,请教大家了
------解决方案--------------------using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.IO.FileStream fs = new System.IO.FileStream(@ "C:\1.txt ", System.IO.FileMode.Open, System.IO.FileAccess.Read);
int size=(int)fs.Length;
byte[] buff = new byte[size];
fs.Read(buff, 0, size);
fs.Close();
System.Text.Encoding en= System.Text.Encoding.GetEncoding(@ "GB2312 ");
string s = en.GetString(buff, 0, size);
System.IO.StringReader sr= new System.IO.StringReader(s);
string[] separator = new string[2];
separator[0] = @ ", ";
separator[1] = @ ", ";
string data;
data = sr.ReadLine();
while (data != null)
{
string[] d = data.Split(separator, StringSplitOptions.None);
string item = d[0].Trim() + @ " " + d[2].Trim();
listBox1.Items.Add(item);
data = sr.ReadLine();
}
}
}
}
------解决方案--------------------给你一人简单参考
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
FileStream fs2 = new FileStream(textBox1.Text + "1 ", FileMode.Create, FileAccess.Write);
fs2.Seek(0, SeekOrigin.Begin);
int i = 0;
StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs2);
try
{
string Line1 = null;
string Line2 = null;
string Temp = null;
while (true)
{
Line1 = sr.ReadLine();
if (Line1.IndexOf( "point1 = new Point ") > 0)
{
Line2 = sr.ReadLine();
i = Line1.IndexOf( "new Point ");
Temp = Line1.Substring(i, Line1.Length - i - 1);
Line2 = Line2.Replace( "point1 ", Temp);
sw.WriteLine(Line2);
}
else if (Line1.IndexOf( "size1 = new Size ") > 0)
{
Line2 = sr.ReadLine();
i = Line1.IndexOf( "new Size ");
Temp = Line1.Substring(i, Line1.Length - i - 1);
Line2 = Line2.Replace( "size1 ", Temp);
sw.WriteLine(Line2);
}
else if (Line1 == "this end ")