3

I have a suite of unit tests I run in Eclipse which all work fine. They depend on data loaded from a very large > 20MB file. However, when I run the unit tests from ANT the tests fail because some of the data is not loaded. What happens is my file reading mechanism does not read the entire file, it just stops , without giving any error after reading about 10,000 of 900,000 lines

Here is my file reading code

    private static void initializeListWithFileContents(
        TreeMap<String, List<String>> treeMap, String fileName)
{
    File file = new File(fileName);
    Scanner scanner = null;
    int count = 0;
    try
    {

        scanner = new Scanner(file);
        while (scanner.hasNextLine())
        {
            String line = scanner.nextLine().toLowerCase().trim();      
            String[] tokens = line.split(" ");

            if (tokens.length == 3)
            {
                String key = tokens[0] + tokens[1];
                if (treeMap.containsKey(key))
                {
                    List<String> list = treeMap.get(key);
                    list.add(tokens[2]);
                }
                else
                {
                    List<String> list = new ArrayList<String>();
                    list.add(tokens[2]);
                    treeMap.put(key, list);
                }
                count++;
            }
        }           
        scanner.close();
    }

    catch (IOException ioe)
    {
        ioe.printStackTrace();
    }
    System.out.println(count + " rows added");
}

This is part of a Web app. The web app also works fine, the entire file gets loaded to memory. If the data my Unit tests depends on are contained in the first 10,000 lines then unit tests pass ok with ANT. The only thing I can think of is it must be a memory issue but why then do I not get an exception thrown. I run my ANT target from within Eclipse. It is configured with the same JVM args as my Eclipse JUnit runner is , ie -Xms512m -Xmx1234m. I know it picks these up correctly because if ANT launches with the default JVM parameters then it will fail with Heap error. Any other ideas what I could check?

4

1 回答 1

1

Scanner类型会吞下 I/O 错误。您必须使用ioException()方法显式检查错误。

如果问题是编码错误,您需要在实例化扫描仪时显式传递文件的编码。

如果文件是损坏的文本文件,您可能需要提供自己的阅读器来进行更多的容错解码。如果可能,应该避免这种情况,因为它不太正确。

于 2012-12-11T11:04:12.643 回答