Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - 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] + "	" + temp[0] + "	"
                            + temp[i] + "	" + files + "
");
                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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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", ""));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...