快速的方法来按字母顺序对java中文件的内容进行排序 [英] Fast way to alphabetically sort the contents of a file in java

查看:317
本文介绍了快速的方法来按字母顺序对java中文件的内容进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以推荐一种快速的方式来排序文本文件的内容,基于每行的前X个字符?
例如,如果我在文本文件中有以下文本:

  Adrian Graham这里有更多文本

John Adams


$ b然后再插入另一条记录, / b>

  Bob这里有更多文字

我需要保持文件排序,但这是一个相当大的文件,我宁愿不加载它一次完全内存。
大的我的意思是大约500 000行,所以也许不是非常巨大。



我搜索了一下,发现 http:/ /www.codeodor.com/index.cfm/2007/5/14/Re-Sorting-really-BIG-files--the-Java-source-code/1208
,我想知道是否有人可以建议任何其他方式?为了有第二个意见?

我读到上面的链接文章之前的初步想法是:

阅读文件



将它分成几个文件,例如A到Z

如果一行以一个然后它被写入到名为A.txt的文件中

每个文件都有自己的内容排序(没有清楚的概念,除了字母顺序外还有多少)



然后说到阅读数据,我知道如果我想找到一个以A开头的行然后我打开A.txt
当插入一个新的行相同的东西适用,我只是追加到文件的末尾。之后插入的时候,有时间我可以调用我的排序程序重新排序已附加到他们的东西的文件。



我意识到有一些缺陷这像例如。不会有一个以特定字母开头的行,所以有些文件可能会比其他文件大。



这又是为什么我需要第二个意见有关如何解决这个问题的建议?
当前的程序是在java中,但任何编程语言都可以用于实现这个的一个例子...我将端口,我需要的。 (如果有人想知道我不是故意通过这种方式存储信息而让自己头痛,我继承了一个痛苦的小程序,它将数据存储到文件而不是使用某种数据库)
提前致谢

解决方案

您也可以简单地调用DOSsort命令对文件进行排序。这是快速的,将需要旁边没有编程的部分。

在DOS框中键入帮助排序|更多用于排序语法和选项。


Can anyone recommend a fast way to sort the contents of a text file, based on the first X amount of characters of each line? For example if i have in the text file the following text

Adrian Graham   some more text here

John Adams     some more text here

Then another record needs to be inserted for eg.

Bob Something some more text here

I need to keep the file sorted but this is a rather big file and i'd rather not load it entirely into memory at once. By big i mean about 500 000 lines, so perhaps not terribly huge.

I've had a search around and found http://www.codeodor.com/index.cfm/2007/5/14/Re-Sorting-really-BIG-files---the-Java-source-code/1208 and i wanted to know if anyone could suggest any other ways? For the sake of having second opinions?

My initial idea before i read the above linked article was:

Read the file

Split it into several files, for eg A to Z

If a line begins with "a" then it is written to the file called A.txt

Each of the files then have their contents sorted (no clear idea how just yet apart from alphabetical order)

Then when it comes to reading data, i know that if i want to find a line which starts with A then i open A.txt When inserting a new line the same thing applies and i just append to the end of the file. Later after the insert when there is time i can invoke my sorting program to reorder the files that have had stuff appended to them.

I realise that there are a few flaws in this like for eg. there won't be an even number of lines that start with a particular letter so some files may be bigger than others etc.

Which again is why i need a second opinion for suggestions on how to approach this? The current program is in java but any programming language could be used for an example that would achieve this...I'll port what i need to.

(If anyone's wondering i'm not deliberately trying to give myself a headache by storing info this way, i inherited a painful little program which stores data to files instead of using some kind of database) Thanks in advance

解决方案

You may also want to simply call the DOS "sort" command to sort the file. It is quick and will require next to no programming on your part.

In a DOS box, type help sort|more for the sort syntax and options.

这篇关于快速的方法来按字母顺序对java中文件的内容进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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