0

我已经编写了用于从文本文件传递整数输入的 Java 代码,例如1 10 39 59 20 60 38,当有空格时我必须拆分字符串。

输入在单行中给出input.txt

我的代码是:

public void reduce(Text key, Iterator<IntWritable> values,
        OutputCollector<Text, IntWritable> output, Reporter reporter)
        throws IOException {

    String calc;
    calc = key.toString();

    ArrayList<Integer> keys = new ArrayList<Integer>();
    String[] data = calc.split(" ");

    for (String s : data) {
        int intData = Integer.parseInt(s);
        keys.add(intData);
    }

    int val = 0;
    for (int a : keys) {
        // some tasks
    }
}

分割线后,我将分离的值用于不同的任务。我的问题是如何拆分位于同一文件中的所有值(值也在不同的行中)并将它们存储在一个数组中?

假设如果以下是input.txt中给出的输入,那么如何拆分所有值并将它们存储在一个数组中?

示例输入:

1 4 92 58 30 82
49 50 38 30 29 20
...

预期输出:

array1="1,4,92,58,30,82,49,50,38,30,29,20, .."

当我将我的代码用于上述输入时,只考虑输入文件的最后一行 - 所有前面的行都被忽略。

4

5 回答 5

1

如果您的类路径中有 commons-io-xx,您可以尝试以下代码。我使用 commons-io-2.4 进行演示。此外,我在这里假设输入为 String,您可以使用 Integer.parseInt(String str) 从输入文件中获取整数值。

package com.stack.overflow.works.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;

/**
 * 
 * @author sarath_sivan
 *
 */

public class FileIOService {

    private static final String BLANK_SPACE = " ";

    public static void main(String[] args) {
        FileIOService.run();
    }

    public static void run() {
        long startTime = System.currentTimeMillis();
        String fileName = "C:/Users/sarath_sivan/Desktop/input.txt";
        FileIOService.display(split(getContent(fileName)));
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println("Total Time: "+ elapsedTime + " Milliseconds...");
    }

    public static void display(List<String> splitsList) {
        for (String split: splitsList) {
            System.out.println(split);
        }
    }

    public static List<String> split(String content) {
        List<String> splitsList = new ArrayList<String>(Arrays.asList(content.split(BLANK_SPACE)));
        return splitsList;
    }

    public static String getContent(String fileName) {
        File file = new File(fileName);
        String content = null;
        try {
            content = FileUtils.readFileToString(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

}

希望这会有所帮助...非常感谢!

于 2013-02-25T17:37:16.970 回答
0

This is pure speculation, but it seems that these lines are the culprits:

ArrayList<Integer> keys = new ArrayList<Integer>();
            String [] data = calc.split(" ");

I assume you are invoking this method inside a for loop, which would of course only give you the last processed line. What you need to do is declare keys outside of the function so that way it isn't reinstantiated on each invocation of the surrounding function.

于 2013-02-25T16:45:55.637 回答
0

你也可以检查calc字符串,我猜它只会包含一行,然后你必须检查

key.toString()

方法。

此外,使用 java 1.7 你可以初始化 ArrayList

ArrayList<Integer> keys = new ArrayList<>();
于 2013-02-25T16:56:57.277 回答
0

您似乎没有在 Text 键参数中连接文件的所有行。

我想您可以使用 Apache FileUtils 以单个字符串 var 读取文件(http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readFileToString(java. io.文件)

于 2013-02-25T17:01:44.350 回答
0

当您读取文件时,您基本上是在调用 readLine() 方法,该方法将为您提供一个包含当前行数据的字符串。但是在您的情况下,当您位于文件末尾时,将读取一个字符串,该字符串将被拆分并分配给字符串数组。所以在这里你以前的价值观会丢失。因此,为了避免这种情况,您必须使用一个临时数组来存储您当前的结果,然后将该结果添加到您的结果数组中。

String[] mainArray=new Array[255]; // 255 for example

String temp;

while((temp=br.readLine()!=null))
{

String[] tempArr=temp.split(" ");
addToMainArray(tempArr);

}

希望这可以帮助。

于 2013-02-25T17:05:14.677 回答