1

I need to read a sample.csv file from the same class as my main. Below you can see the structure.

enter image description here This is my code but when I run it it gave me the error that the sample.csv is not find!

public int convertFile(String csvFileName, String xmlFileName,
                    String delimiter) {
        int rowsCount = -1;
        BufferedReader csvReader;
        try {
            Document newDoc = domBuilder.newDocument();
            // Root element
            Element rootElement = newDoc.createElement("XMLCreators");
            newDoc.appendChild(rootElement);
            // Read csv file
            csvReader = new BufferedReader(new FileReader(csvFileName));
            CSVReader reader = new CSVReader(new FileReader("sample.csv"));
            //CSVReader reader = new CSVReader(csvReader);
            String[] nextLine;
            int line = 0;
            List<String> headers = new ArrayList<String>(5);
            while ((nextLine = reader.readNext()) != null) {
....

Can anyone suggest me what is the problem?

1 Answer 1

1

Without relative or absolute path, it is directly related to the "current" directory. So it does NOT matter so much that the .csv file is in a certain package, but it does matter that when the application is started the "working" directory is exactly the directory that contains the file. You are using Eclipse, go to see the Run Configuration (Run menu -> Run Configurations ...) used to launch the application, Arguments tab. See Attachment for same

you can get path like this.

URL url = YourClassName.class.getResource("sample.csv");
FileReader fileReader = new FileReader(url.getFile());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.