请注意FILENAME以及如何打印出我从我的电脑中选择的文件名?

Please pay attention to the FILENAME and how to print out the filename that I choose from my computer?

很简单的问题,如何打印出选中的文件名,谢谢。

public class CSVMAX {
public CSVRecord hottestInManyDays() {
//select many csv files from my computer
   DirectoryResource dr = new DirectoryResource();
   CSVRecord largestSoFar = null;
//read every row and implement  the method we just define
   for(File f : dr.selectedFiles()) {
    FileResource fr = new FileResource(f);
    CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
    if (largestSoFar == null) {
       largestSoFar = currentRow;
    }
    else {
    double currentTemp = Double.parseDouble(currentRow.get("TemperatureF"));
        double largestTemp = Double.parseDouble(largestSoFar.get("TemperatureF"));
        //Check if currentRow’s temperature > largestSoFar’s
    if (currentTemp > largestTemp) {
                //If so update largestSoFar to currentRow
            largestSoFar = currentRow;
    }
   }
}
return largestSoFar;        
}

这里我想打印出文件名,但是我不知道怎么打印。

public void testHottestInManyDay () {
    CSVRecord largest = hottestInManyDays();
    System.out.println("hottest temperature on that day was in file " + ***FILENAME*** + largest.get("TemperatureF") +
               " at " + largest.get("TimeEST"));
}
}

参考行for(File f : dr.selectedFiles())

f 是一个 [File]. It has a toString() 方法 [来自文档],

Returns the pathname string of this abstract pathname. This is just the string returned by the getPath() method.

所以,在循环的第一行,你可以输入System.out.println(f.toString());来打印出文件路径。

希望这有助于澄清部分故事。

现在,要更新此字符串,我看到您正在使用一些在 testHottestInManyDay() 中称为 largest 的对象。您应该在此对象中添加文件路径字符串并将其设置在 else 块中。

最终,hottestInManyDays()似乎需要return这个信息。

CSVRecord 有 属性 吗?

像这样:

CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
currentRow.setFileName(f.getName());

如果没有,可以加这样一个属性吗?

也许 CSVRecord 没有那个 属性。但是可以加吗?:

private String _fileName;

public void setFileName(String fileName) {
    this._fileName = fileName;
}

public String getFileName() {
    return this._fileName;
}

如果没有,你能为这两条信息创建一个包装器class吗?

如果您无法修改 CSVRecord 并且它没有您想要的信息的位置,请将其包装在 class 中。就这么简单:

class CSVWrapper {

    private CSVRecord _csvRecord;
    private String _fileName;

    // getters and setters for the above
    // maybe also a constructor?  make them final?  your call

}

然后 return 那个 来自 hottestInManyDays() 而不是 CSVRecord。像这样:

CSVWrapper csvWrapper = new csvWrapper();
csvWrapper.setCSVRecord(currentRow);
csvWrapper.setFileName(f.getName());

当然,根据需要更改方法签名和 return 值。


无论你怎么做,一旦它位于 hottestInManyDays() 的 return 值上,你就可以在消耗它的方法中使用它:

CSVWrapper largest = hottestInManyDays();
System.out.println("hottest temperature on that day was in file " + largest.getFileName() + largest.getCSVRecord().get("TemperatureF") +
           " at " + largest.getCSVRecord().get("TimeEST"));

(注意:如果最后的位不符合 Demeter 法则,请随意扩展包装器以根据需要包含直通操作。甚至可以共享一个与 CSVRecord 的通用接口,因此它可以用作系统其他地方需要的接口的直接替代品。)

必须 return CSVRecord 和文件。要么在新制作的 class 中。 由于 CSVRecord 可以转换为地图,因此将文件名添加到地图中,使用新的列名,此处 "FILENAME."

public Map<String, String> hottestInManyDays() {
    //select many csv files from my computer
    DirectoryResource dr = new DirectoryResource();
    CSVRecord largestSoFar = null;
    File fileOfLargestSoFar = null;
    //read every row and implement  the method we just define
    for (File f : dr.selectedFiles()) {
        FileResource fr = new FileResource(f);
        CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
        if (largestSoFar == null) {
            largestSoFar = currentRow;
            fileOfLargestSoFar = f;
        }
        else {
            double currentTemp = Double.parseDouble(currentRow.get("TemperatureF"));
            double largestTemp = Double.parseDouble(largestSoFar.get("TemperatureF"));
            //Check if currentRow’s temperature > largestSoFar’s
            if (currentTemp > largestTemp) {
                //If so update largestSoFar to currentRow
                largestSoFar = currentRow;
                fileOfLargestSoFar = f;
            }
        }
    }
    Map<String, String> map = new HashMap<>(largestSoFar.toMap());
    map.put("FILENAME", fileOfLargestSoFar.getPath());
    return map;
}

Map<String, String> largest = hottestInManyDays();
System.out.println("hottest temperature on that day was in file " 
        + largest.get("FILENAME") + largest.get("TemperatureF") +
           " at " + largest.get("TimeEST"));