【问题标题】:I need to open a text file that contains integers and i need to print out the smallest and lowest numbers in the file我需要打开一个包含整数的文本文件,我需要打印出文件中最小和最小的数字
【发布时间】:2016-03-11 01:36:56
【问题描述】:

这是我到目前为止所得到的。我不知道如何将最低和最高数字打印到屏幕上。任何帮助将不胜感激。谢谢。

BufferedReader openFile;
    try{
        openFile = new BufferedReader(new FileReader("LABEX10.txt"));
    }
    catch(FileNotFoundException e){
        System.out.println("Could not open file LABEX10.txt");
        System.exit(0);
    }catch(IOException ex){
        System.err.println(ex);
    }
}

}

【问题讨论】:

标签: java


【解决方案1】:

使用 Java 8:

final Path path = Paths.get("LABEX10.txt");
try (Stream<String> lines = Files.lines(path)) {
    final LongSummaryStatistics summary = lines
            .mapToLong(Long::parseLong)
            .summaryStatistics();
    System.out.printf("range [%d .. %d]%n", summary.getMin(), summary.getMax());
}

这种方法比proposed duplicate 更现代。

【讨论】:

    猜你喜欢
    • 2016-04-03
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2012-02-16
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多