将txt文件导入文本框 [英] Import of txt file into textbox

查看:112
本文介绍了将txt文件导入文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有一个导入到程序中的文本文件.我的代码可以在普通文本文件上正常工作,但是问题是我从客户端获取了由于某种原因不能正确导入的文本文件(仅导入第一行).

如果我在记事本中打开文本文件并另存为"文本文件,然后尝试再次将其导入,则可以正常工作.

我不能总是打开文件并再次保存它,因为这会减慢生产速度,并且文本文件的平均大小为100mb.因此,我需要很长时间才能在记事本中打开并再次保存.

还有另一种导入文件的方法吗?
这是我使用的代码:

Hi all.

I have a text file that I import into my program. My code works fine with a normal text file but the problem is I get text files from a client that for some reason doesn''t import properly(It only imports the first line).

If I open the text file in notepad and "save as" a text file and then try to import it again it works fine.

I can''t always open the file and save it again as this slows down production and the text file is on average 100mb. So it takes a long time for me to open in notepad and save again.

Is there another way of importing the file?
Here is the code I use:

Dim FILE_NAME As String = TextBox1.Text
Dim objReader As New System.IO.StreamReader(FILE_NAME)
TextBox2.Text = TextBox2.Text & objReader.ReadToEnd

推荐答案

您是在认真地说要在文本框中加载100MB吗?
为什么?
您希望读谁呢?甚至带有PDF格式的合理大小的.NET手册(带图片)也只有其十分之一左右.一个100MB的文本文件约为300万行文本.
这可怜的草皮要读多久?一周?一年吗?

找到一个更好的方法:为他分页,提供搜索功能,除了打开一个巨大的文件并塞住用户"以外的任何内容!
Are you seriously saying that you are loading 100MB into a text box?
WHY?
Who the heck do you expect to read that? Even an reasonable size .NET manual in PDF format, with pictures, is about a tenth the size. A 100MB text file is approximately 3 million lines of text.
How long is the poor sod being given to read it? A week? A year?

Find a better way: page it for him, provide search facilities, anything but "open a huge file and stuff the user"!


尝试在循环中一次读取一行.就像这里显示的示例一样:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx [ ^ ]

不要连接以下内容:
Try reading it one line at a time inside a loop. like in the example shown here:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx[^]

Do not concatenate something like:
s = s & sLine



但是使用类似StringBuilder的方法:



but use a StringBuilder like:

objStringBuilder.Append(sLine)



另外,如果您已经知道文件大小,则可以使用确保容量来保留所需的内存.

祝你好运!



Also, if you already know the file size you can use EnsureCapacity to reserve the memory needed.

Good luck!


对不起,我应该提到没有人可以读取文件.我们需要文本文件中的信息,程序会拆分数据,然后进行计数并生成一个漂亮的小报告.该程序读取文本文件,因此用户不必:cool:

该报告只是说产品A = 375,产品B = 645,依此类推.

Thanx E.F. Nijboer.我将看看字符串生成器;)
Sorry I should have mentioned that no one is going to read the file. We need info from the text file and the program splits up the data and just does a count and wrights a nice small report. The program reads the text file so a user doesn''t have to :cool:

The report is just going to say product A = 375 and product B = 645 and so on.

Thanx E.F. Nijboer. I''ll have a look at the string builder ;)


这篇关于将txt文件导入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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