0

For example, I have two CSV files in folder and need to read data from one file line by line, and store it in array list A like file 2 to array list B dynamically.. i.e. if there is 3 file it should store in array list C

public class DashboardReport
{
    public static void main(String[] args)
    {
        int count = 0;
        String line = "";

        File folder = new File("D:/April");
        File[] listOfFiles = folder.listFiles();

        System.out.println("Count" + listOfFiles.length);
        count = listOfFiles.length;

        List books = new ArrayList();

        for (int i = 0; i <= listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                System.out.println("File " + listOfFiles[i].getName());
                Path pathToFile = Paths.get(listOfFiles[i].getName());

                try (BufferedReader br = Files.newBufferedReader(
                    pathToFile, StandardCharsets.US_ASCII))
                {
                    line = br.readLine();
                    String[] attributes = {};

                    while (line != null)
                    {
                        attributes = line.split(",");
                        books.add(attributes);

                        line = br.readLine();
                    }
                }
                catch (IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
            else if (listOfFiles[i].isDirectory())
            {
                System.out.println("Directory " + listOfFiles[i].getName());
            }
        }
    }
}
3
  • 2
    It would helpful if you can add what you have tried and where you are stuck or having issue. Commented May 8, 2016 at 4:58
  • Getting an error D:\Nandan KT Doc\Java code>java DashboardReport Count2 File April_rworld_node1_1.log.csv java.nio.file.NoSuchFileException: April_rworld_node1_1.log.csv at sun.nio.fs.WindowsException.translateToIOException(WindowsException.j ava:79) Commented May 8, 2016 at 5:03
  • @NandanShastry Please include the errors in the post itself, and learn to use indentation. Commented May 8, 2016 at 5:04

1 Answer 1

1

You have two errors here. One is in the for loop.

for (int i = 0; i <= listOfFiles.length; i++)
                   ^

While iterating on an array, you'd normally do iterate from 0 to length - 1 not 0 to length.

Then the second one is you are not taking the path into account, only the filename.

Path pathToFile = Paths.get(listOfFiles[i].getName());
                                           ^

This searches for a file with the same name, but in the current working directory, and as usual, it won't be found. Change it to use the absolute path instead.

Path pathToFile = Paths.get(listOfFiles[i].getAbsolutePath());

Now you will be getting the file from that D:\April\ directory, where your file does exist.

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.