0

I'm trying to write a static function that reads a CSV and output a value from that said CSV. I'm having trouble reading it because every time I get a fileNotFound exeception, however I believe my path is correct.

Here's the code

public class DateFormatUtils {

  private static String dateFormatCsvPath = "csv/country_date_format.csv";

  public static String getDateFormatByLocale(Locale Locale) {
    List<Map<String, String>> mapList = Collections.emptyList();
    String fileExtension = "csv";
    try {
      InputStream inputStream = new FileInputStream(dateFormatCsvPath);
      mapList = ExcelUtils.read(inputStream, fileExtension);
      String a = "a";

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return "a";
  }
}

Here's a screenshot of my project, you can see that relatively from my class, the file is under csv/country_date_format.csv. I also tried ./csv/country_date_format.csv and /country_date_format.csv with no avail.

I also tried to get my file as a resource but without success too as such
String path = DateFormatUtils.class.getClass().getResource("csv/country_date_format.csv").getPath()

I don't understand why I still get the file not found exception. Anyone can enlighten me?

The csv and the rest of the code is bundled inside a JAR

enter image description here

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Jorel Amthor
  • 1,124
  • 11
  • 34
  • I suggest right-mouse-click on the csv file then choose 'File path' and then copy the path in the explorer screen that opens, paste this in your code and change all the \ to / – Jens van Groeningen Mar 05 '21 at 15:36
  • use `String currentWorkingDir = System.getProperty("user.dir");` to get running application working dir and specify path including that. – onkar ruikar Mar 05 '21 at 15:36
  • @onkarruikar that would help me build an absolute path but id rather use a relative one – Jorel Amthor Mar 05 '21 at 15:49
  • @Abra that would help me build an absolute path but id rather use a relative one – Jorel Amthor Mar 05 '21 at 15:49
  • Current working dir returned by the API will change when you change the executable location. You are not hard coding the absolute path this way. – onkar ruikar Mar 05 '21 at 15:50
  • @onkarruikar the `System.getProperty("user.dir")` gives me the path of the folder when my JAR is aka `/User/proj/jar` instead of the root of my project which is `/User/proj/code/` and eventhough it would give me my working directory, there is a lot of folders from the root `/User/proj/code/` to my CSV file, so i will have no choice but to hardcode like `System.getProperty("user.dir") + /folderA/folderB/folderC/folderD/ + fileName`. Hence why im looking for something relative and not absolute. – Jorel Amthor Mar 05 '21 at 15:58
  • `InputStream input = getClass().getResourceAsStream("/com/mycompany/utils/csv/xyz.csv");` In OP please mention that the file is inside a jar. – onkar ruikar Mar 05 '21 at 16:06
  • @onkarruikar this looks very hardcoded to me :/ – Jorel Amthor Mar 05 '21 at 16:19
  • Does this answer your question? [How to get a path to a resource in a Java JAR file](https://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file) – onkar ruikar Mar 05 '21 at 16:22

0 Answers0