2016-04-12 12 views
0

這是一個簡單的問題,如何打印出所選的文件名,謝謝。請注意FILENAME以及如何打印出我從我的電腦中選擇的文件名?

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")); 
} 
} 
+1

「CSVRecord」是否包含該信息?如果沒有,該屬性是否可以添加到'CSVRecord'?如果沒有,你可以創建一個本身包含'CSVRecord'的類以及你想要的其他信息,並從'hottestInManyDays()'返回* *。 – David

+1

你不能。您的實際操作只返回CSVRecord。您至少已經返回了文件。 –

回答

2

最終,看來hottestInManyDays()將需要返回這個信息。

請問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; 
} 

如果沒有,你可以創建兩條信息的包裝類?

如果你不能修改CSVRecord,它沒有你想要的信息的地方,將它包裝在一個類中。事情如此簡單:

class CSVWrapper { 

    private CSVRecord _csvRecord; 
    private String _fileName; 

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

} 

然後返回hottestInManyDays()代替CSVRecord。例如:

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

當然,根據需要更改方法簽名和返回值。

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")); 

(注:如果在該位結尾處,有唐


但是你這樣做,一旦它的返回值從hottestInManyDays()您可以在消耗,該方法使用如果Demeter違反法律,則可以隨意擴展包裝以包含pass-thru操作,甚至可以與CSVRecord共享一個通用接口,因此它可以用作一個接口的替代品如系統中其他地方所需。)

+0

感謝大衛。還有一個問題,最後我得到了「C:\ Users \ 2015044 \ Desktop \ BlueBlueJ \ data \ 2015 \ weather-2015-01-01.csv」。我想如何得到「weather-2015-01-01.csv」而不是整個路徑?謝謝。 –

+0

@YottaLynn:http://stackoverflow.com/questions/14526260/how-do-i-get-the-file-name-from-a-string-containing-the-absolute-file-path – David

+0

非常感謝大衛。 –

1

參照線for(File f : dr.selectedFiles())

f是[File]。它有一個toString()方法[來自文檔],

返回此抽象路徑名的路徑名字符串。這只是 getPath()方法返回的字符串。

因此,在循環的第一行中,您可以將System.out.println(f.toString());打印出文件路徑。

希望這有助於清除故事的一部分。

現在,要更新此字符串,我看到您正在使用testHottestInManyDay()中名爲largest的某個對象。您應該在此對象中添加一個文件路徑字符串,並將其設置在else塊內。

1

必須同時返回CSVRecord和文件。無論是在新創的課程中。 由於可以將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")); 
相關問題