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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…