0

I want to use relative path in my code but relative is not working for me but working on absolute path. here my code(Utils.java

package org.example;

import org.example.model.Stats;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Utils {
    private static final String FILE_PATH = "/home/project/Dummy/src/main/resources/data.csv";
    public  static List<Stats> readData() {
        List<Stats> StatsList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(
                new FileReader(FILE_PATH))
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(",");

                Stats Stats = new Stats();

                Stats.setName(fields[0]);
                Stats.setAddress(fields[1]);
                Stats.setContact(fields[2]);
                Stats.setAbout(fields[3]);
                

                StatsList.add(Stats);
            }
        } catch (IOException e) {
            System.out.println("Error opening file: " + e.getMessage());
        }
        return StatsList;
    }

In below i give the image of my project structure

Project Structure

  • Did you go through this thead : https://stackoverflow.com/questions/3844307/how-to-read-file-from-relative-path-in-java-project-java-io-file-cannot-find-th – Ashish Kathait Dec 30 '22 at 07:05
  • You should use `this.getClass().getResourceAsStream("/data.csv")` instead... – khmarbaise Dec 30 '22 at 08:29

0 Answers0