如何打开某目录下的全部txt文件,并读取这些文件中的数据?

ybgiser 2009-07-17 09:20:36
对MIS的东西部熟悉,以前也没写过,希望大家多多帮忙啊。现在要实现的功能是,我在界面的textbox里输入一个路径,然后计算机能搜索出在这个路径下的所有.txt文件,并读取其中的数据,按照标示不同存放进数组中,后面并能按照标示取出相应的数据,其中数据格式是这样的:
POLYGON(面标示)
36345.082031 11517.118164 0.000000(x,y,z坐标)
36374.652344 11547.653320 0.000000
36368.117188 11553.867188 0.000000
36338.332031 11523.546875 0.000000

POINT(点)
41002.476563 10052.284180 0.000000(x,y,z坐标)

。。。。。。。

请大家帮忙啊,谢谢,在线等

...全文
272 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-07-17
  • 打赏
  • 举报
回复
这样?

private void button2_Click(object sender, EventArgs e)
{
string[] _FileList =System.IO.Directory.GetFiles(@"C:\Temp","*.TXT",System.IO.SearchOption.AllDirectories);

for(int i=0;i!=_FileList.Length;i++)
{
Test.LoadFile(_FileList[i]);
}

}


public class Test
{
public static IList<Class1> _POLYGON = new List<Class1>();

public static IList<Class1> _POINT = new List<Class1>();


public static void LoadFile(string p_File)
{
string[] _List = System.IO.File.ReadAllLines(p_File);

IList<Class1> _Add = _POLYGON;
for (int i = 0; i != _List.Length; i++)
{
switch (_List[i].ToUpper().Trim())
{
case "POLYGON":
_Add = _POLYGON;
break;
case "POINT":
_Add = _POINT;
break;
default:
_Add.Add(new Class1(_List[i]));
break;
}
}

}

public class Class1
{
private decimal m_X = 0;
private decimal m_Y = 0;
private decimal m_Z = 0;


public decimal X { get { return m_X; } set { m_X = value; } }
public decimal Y { get { return m_Y; } set { m_Y = value; } }
public decimal Z { get { return m_Z; } set { m_Z = value; } }

public Class1(string p_Value)
{
string[] _List =p_Value.Split(' ');

X=decimal.Parse(_List[0].Trim());
Y=decimal.Parse(_List[1].Trim());
Z=decimal.Parse(_List[2].Trim());
}

}
}

具体验效自己写把.
tyCode 2009-07-17
  • 打赏
  • 举报
回复
很好
kkun_3yue3 2009-07-17
  • 打赏
  • 举报
回复
帮你总结一下:
问题1:编历某目录下所有txt文件
问题2:读取某文件的内容
问题3:按照标示不同放到不同的数组里(标识不详)
问题4:根据约定取相应的数据(约定不详)


问题1代码
            string path = "C:\\360safebox";
string[] fileInfo = Directory.GetFiles( path, "*.dat", SearchOption.AllDirectories );
for( int i = 0 ; i < fileInfo.Length-1 ; i++ ) {
textBox1.AppendText( fileInfo[ i ] + Environment.NewLine );
}


问题2:读取某文件的内容
            string filename = "c:\\a.txt";
using( FileStream fs = new FileStream( filename, FileMode.Open ) ) {
byte[] con = new byte[ fs.Length ];
fs.Read( con, 0, (int)fs.Length );
textBox1.AppendText( Encoding.Unicode.GetString( con ) );
}


问题3:按照标示不同放到不同的数组里(标识不详)
//
问题4:根据约定取相应的数据(约定不详)
//
falx2004 2009-07-17
  • 打赏
  • 举报
回复

//这个是个窗体类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace TextReader
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

string _fileFolder="";
public string fileFolder
{
get
{
return _fileFolder;
}
}

bool ishavePage = false;
string _fileName = "";
public string fileName
{
get
{
return _fileName;
}
}


string _fileFullName = "";
public string fileFullName
{
get
{
return _fileFullName;
}
}

List<string> fileFullNameList = new List<string>();
private void btnAddNext_Click(object sender, EventArgs e)
{
bool added = false;
if (fileFullNameList==null||fileFullNameList.Count==0)
{
DirectoryInfo di = new DirectoryInfo(_fileFolder);
if (!di.Exists)
{
return;
}
FileInfo[] fi = di.GetFiles();
foreach (FileInfo file in fi)
{
string t= file.FullName;
string[] tlist= t.Split('.');

if (tlist[tlist.Length-1].ToLower()=="txt")
{
fileFullNameList.Add(file.FullName);
}

}
}
for (int i = 0; i < fileFullNameList.Count; i++)
{
if (added)
{
return;
}
string[] temp = fileFullNameList[i].Split('\\');
string tempFileName = temp[temp.Length - 1];
bool isfileOpened = false;
for (int j = 0; j < tabControl1.TabPages.Count; j++)
{
if (tabControl1.TabPages[j].Text==tempFileName)
{
isfileOpened = true;
break;
}
}
if (!isfileOpened)
{
textBox t = new textBox();
t.setText(readTxtFile(fileFullNameList[i], ""));
tabControl1.TabPages.Add(tempFileName);
if (tabControl1.TabPages.Count != 0)
{
for (int k = 0; k < tabControl1.TabPages.Count; k++)
{
if (tabControl1.TabPages[k].Text == tempFileName)
{
// tabControl1.TabPages[k].Focus();
tabControl1.SelectTab(tabControl1.TabPages[k]);
tabControl1.TabPages[k].Controls.Add(t);
tabControl1.TabPages[k].Controls[0].Dock = DockStyle.Fill;
added = true;
}
}
}
}
}




}

private void btnOpen_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
_fileFullName = openFileDialog1.FileName;
string[] temp = fileFullName.Split('\\');
_fileName = temp[temp.Length - 1];
string temstr=_fileFullName;
_fileFolder = temstr.Remove(_fileFullName.Length - _fileName.Length-1);
}
textBox t = new textBox();
t.setText(readTxtFile(_fileFullName, ""));
if (!ishavePage)
{
TabPage page = new TabPage();
page.Text = fileName;
tabControl1.TabPages.Add(page);
if (tabControl1.TabPages.Count!=0)
{
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i].Text==fileName)
{
tabControl1.TabPages[i].Controls.Add(t);
tabControl1.TabPages[i].Controls[0].Dock = DockStyle.Fill;
}
}
}
ishavePage = true;

}
else
{
tabControl1.TabPages.Add(fileName);
if (tabControl1.TabPages.Count != 0)
{
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
if (tabControl1.TabPages[i].Text == fileName)
{
tabControl1.TabPages[i].Controls.Add(t);
tabControl1.TabPages[i].Controls[0].Dock = DockStyle.Fill;
}
}
}

}

}
public ArrayList readTxtFile(string path, string fgf)
{
if (!File.Exists(path))
{
Console.WriteLine("文件不存在!");
Console.WriteLine("按回车键退出!");
Console.ReadLine();
return null;
}
try
{
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
string l;
ArrayList content = new ArrayList();
while ((l = sr.ReadLine()) != null)
{
content.Add(l);
}
sr.Close();
content.TrimToSize();
return content;
}
catch (IOException ex)
{
Console.WriteLine("读文件出错!请检查文件是否正确。");
Console.WriteLine(ex.ToString());
return null;
}
}


}
}
//这个是才窗体中控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace TextReader
{
public partial class textBox : UserControl
{
public textBox()
{
InitializeComponent();
}

public string fileName="";
public string fileFolder = "";
private void toolStripButton1_Click(object sender, EventArgs e)
{
}
public void setText(ArrayList s)
{
for (int i = 0; i < s.Count; i++)
{
if (this.boxReport.Text.Length==0)
{
boxReport.Text += s[i].ToString();
}
else
{
boxReport.Text += Environment.NewLine + s[i].ToString();
}
}
}

private void textBox_DragDrop(object sender, DragEventArgs e)
{

}

}
}




由于经常使用记事本...然后到处都是....然后就写了个简陋的东西
方便浏览所有记事本
qqiuzaihui 2009-07-17
  • 打赏
  • 举报
回复
//读取单个文件的一个事例, 自己研究下。 把指定的单个文件换成 Directory.GetFiles() 就行了。

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

#endregion

namespace CommaValues
{
class Program
{
private static List<Dictionary<string, string>> GetData(out List<string> columns)
{
string strLine;
string[] strArray;
char[] charArray = new char[] { ','}; //以逗号分隔,换成以空格分隔就行了
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
columns = new List<string>();

try
{
FileStream aFile = new FileStream("../../../SomeData.txt", FileMode.Open);
StreamReader sr = new StreamReader(aFile);

// Obtain the columns from the first line //从文件的第一行中获取列标
// Split row of data into string array
strLine = sr.ReadLine();
strArray = strLine.Split(charArray);

for (int x = 0; x <= strArray.GetUpperBound(0); x++) //获取 Array 的指定维度的上限。
{
columns.Add(strArray[x]);
}

strLine = sr.ReadLine();
while (strLine != null)
{
// Split row of data into string array
strArray = strLine.Split(charArray);
Dictionary<string, string> dataRow = new Dictionary<string, string>(); //Dictionary表示键和值的集合

for (int x = 0; x <= strArray.GetUpperBound(0); x++) //集合中的每一项都有一个对应于列名的键和一个值
{
dataRow.Add(columns[x], strArray[x]);
}

data.Add(dataRow);
strLine = sr.ReadLine();
}

sr.Close();
return data;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return data;
}
}

static void Main(string[] args)
{
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);

foreach (string column in columns)
{
Console.Write("{0,-20}", column);
}
Console.WriteLine();

foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0,-20}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}


/*************SomeData.txt中的数据:***************
ProductID,Name,Price
1,Spiky Pung,1000
2,Gloop Galloop Soup,25
4,Hat Sauce,12
**************************************************/
lzc2125 2009-07-17
  • 打赏
  • 举报
回复
思路:
1.寻找路径下所有的.txt文件
2.遍历所有文件,一个一个的读取
3.遍历的同时进行相关逻辑处理,比如:去标示数据

就可以了,每一步都需要相关技术,慢慢调查

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧