Skip to content
Advertisement

Get filename while reading csv

I want to read values from csv files, and order them in a table (console output).

How can I output all files in a folder and read all content in this files, and get filename while reading files with the content in it? I have so far only this, but I can’t become the filename in right way, I become only the last filename and not the content of this file.

public static List<Objekt> run() throws IOException {

    String path2 = "D:\folder\files";
    File folder = new File(path2);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++){
        if (listOfFiles[i].isFile()){
            files = listOfFiles[i].getName();
            if (files.endsWith(".csv")){
                files = files.replace(".csv", "");
                System.out.println(files);
            }
        }
    }

    List<Objekt> lines = new ArrayList<Objekt>();
    String csvString = "D:\folder\files\file1.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ";";
    Objekt objekt = null;
    String[] hdr = null;
    int l_count = 0;
    br = new BufferedReader(new FileReader(csvString));

    while ((line = br.readLine()) != null) {
        if (l_count == 0) {
            hdr = line.split(cvsSplitBy);
        }
        else{
            String[] temp = line.split(cvsSplitBy);
            for (int i = 0; i < temp.length; i++) {
                objekt = new Objekt();
                objekt.setTimestamp(hdr[i] + "t" + temp[0] + "t"
                            + temp[i] + "t" + files + "n");
                lines.add(objekt);
            }
            System.out.println(lines);
        }
    l_count++;
    }

    br.close();
    return lines;
}

This is what I become (I get only that filename, which is at the end of the folder).

>tr_klue    06.03.2014 11:30    1389    outfilename
>tr_klue_lo 06.03.2014 12:00    1889    outfilename

but I need all filenames in this folder with corresponding content and save these in subfolder with filename and datetime with time when this was read, like:

tr_klue    06.03.2014 11:30    1389    outfilename
>tr_klue_lo 06.03.2014 12:00    1889    outfile1
>tr_klue    06.03.2014 12:30    100 props2
>tr_klue_lo 06.03.2014 13:00    89  colorak

Can you please give me some suggestions in which way to go?

Advertisement

Answer

If I understand your question, you need to first build a List of files and then iterate it –

File[] fileArray = folder.listFiles();
List<String> files = new ArrayList<String>(); // <-- A list of files
for (int i = 0; i < fileArray.length; i++)
{
    if (fileArray[i].isFile())
    {
        String fileName = fileArray[i].getName();
        if (fileName.endsWith(".csv"))  // <-- does it end in ".csv"?
        {
            files.add(fileName);       // <-- Add the file to the List.
        }
    }
}
// Now files contains the matching fileNames...
for (String fileName : files) {
    // Add code here to use each fileName
    System.out.println(fileName.replace(".csv", ""));
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement