我如何从.txt文件中检索数据 [英] How i can retrieve data from .txt file

查看:143
本文介绍了我如何从.txt文件中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从.txt文件中检索数据(注意:按预期顺序),这意味着我希望特定数据不是所有文件,不是所有行都只有一个或多个单词,因此我可以在日志系统的统计信息中使用它. class ="h2_lin">解决方案

hi
可能对您有帮助

获取CSV数据并使用asp保存到数据库中. NET与C#


文本文件不善于在此提取一些内容-它们实际上是旨在从头到尾读取的整体文件-它们没有概念除非您通过代码实现,否则随机访问"或什至是行".

因此,仅提取少量文本将是一件痛苦的事情!有两种基本方法:
1)将整个文件读取为行,然后分别进行处理
2)阅读整个文件(或成块阅读),并使用正则表达式来识别和提取您感兴趣的区域.

如果文件内容包含某种组织(例如XML或类似内容),则还有许多其他方法.

您想要哪个取决于文件内容是什么样,以及要提取什么样的位.

很抱歉,我含糊不清,但实际上并没有解决所有问题的方法.




在这里,我将代码阅读器分配给数据表之后,使用流阅读器从.txt文件中检索一些信息并将其绑定到网格视图,以提供一些代码片段.

<pre lang="c#"> using (FileStream s = new FileStream("C:/TextFileToGridView.txt", FileMode.Open))
        using (StreamReader r = new StreamReader(s))
        {
            List<Object> ds = new List();

            while (!r.EndOfStream)
            {
                String l = r.ReadLine();
                if (l[0] != ''%'')
                {
                    String p = l.Replace("   ", ";");
                    String[] a = p.Split('';'');
                    if (a[0] != "")
                    {
                        ds.Add(new { c0 = a[0], c1 = a[1], c2 = a[2], c3 = a[3] });
                    }
                }
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }



如果您需要任何澄清,请还原.

谢谢
tapan kumar


How could i retrieve data from .txt file (Note: by expected order), mean that i want a specific data not all file not all line only word or more so i can use it in statistics in log system.

解决方案

hi
it may help full to you

Get the CSV data and save into data base using asp.net with c#


Text files are not good at extracting a little bit here and there - they are really monolithic files inteneded to be read from beginning to end - they have no concept of "random access", or even of "lines" unless you code implements it.

So extracting a little bit of the text only is going to be a pain! There are two basic ways:
1) Read the whole file as lines, and process them individually
2) Read the whole file (or as chunks) and use a regex to identify and extract the areas you are interested in.

There are many other ways if the file content contains some organisation, such as XML or similar.

Which you want depends on what your file content looks like, and what kind of bits you want to extract.

Sorry to be horribly vague, but there really isn''t a "one size fits all" solution to this.


Hi,

Here I am giving some code snipet using stream reader to retrive some information from a .txt file and binding it to a grid view, after assigning those information to a datatable.

<pre lang="c#"> using (FileStream s = new FileStream("C:/TextFileToGridView.txt", FileMode.Open))
        using (StreamReader r = new StreamReader(s))
        {
            List<Object> ds = new List();

            while (!r.EndOfStream)
            {
                String l = r.ReadLine();
                if (l[0] != ''%'')
                {
                    String p = l.Replace("   ", ";");
                    String[] a = p.Split('';'');
                    if (a[0] != "")
                    {
                        ds.Add(new { c0 = a[0], c1 = a[1], c2 = a[2], c3 = a[3] });
                    }
                }
            }
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }



If you want any clarification please revert.

Thanks
tapan kumar


这篇关于我如何从.txt文件中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆