【发布时间】:2012-12-17 03:31:21
【问题描述】:
我正在开发一个程序来从 .txt 文件中读取 Wikipedia 页面视图统计文件,到目前为止,我有一个加载方法可以读取该文件,如下所示:
public void loadPVSF(String x) throws FileNotFoundException, IOException {
FileInputStream f = new FileInputStream(x); //obtains bytes from an input file
DataInputStream in = new DataInputStream(f); //reads primitive java types
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((temp = br.readLine()) != null) {
tempArray = temp.split("\n"); //adds each line to an array tempArray
for (String st : tempArray) //puts each element of tempArray through String st
{
MainArray = st.split(" "); //adds each string after a " " to MainArray
for (String str : MainArray) {
if(linecounter<5){
linecounter++;
System.out.println(linecounter + ": " + str);
运行它,这是以下命令行输出的示例:
1: commons.m
2: Category:Gracie_Gold
3: 1
4: 7406
1: commons.m
2: Category:Grad_Maribor
3: 1
4: 7324
1: commons.m
2: Category:Grade_II*_listed_houses_in_Cheshire
3: 1
4: 7781
基本上每组四行是:
1 - Language/Project
2 - Article Title
3 - Number of Page views
4 - Size of the Page (bytes)
我需要知道如何正确分配这些读入行中的每一行。 本质上,我最终需要的是一个哈希表,它将存储文章标题的列表及其相应的查看次数,以便我可以确定哪个查看次数最多。
任何提示或建议将不胜感激。
输入 .txt 文件示例:
nl Andreas_(使徒)7 103145 nl Andreas_Baader 4 46158 nl Andreas_Bjelland 2 28288 nl Andreas_Burnier 2 11545 nl Andreas_Charles_van_Braam_Houckgeest 1 10373 nl Andreas_Eschbach 1 365 nl Andreas_Grassl 1 365
【问题讨论】:
-
您能发布几行 INPUT .txt 文件的示例行吗?
-
您将创建一个类来表示每条记录,为每一行创建该类的一个实例,将值放入其中,然后做任何您想做的事情。具体是什么问题?
-
感谢 Dave 的回复,我卡住的地方是编码下一步。我想为第 1-4 行中的每一行分配正确的对象。每一行 1 将是一个表示语言的字符串,每一行 2 将是一个表示文章标题的字符串,等等。我最终需要的是一个哈希表,其中包含每个文章标题 (2) 及其相对视图数(3) 这样我就可以在数千篇文章之后确定哪篇文章的浏览量最高。我将发布上面输入的示例。