I also wrote that article. That article is about large files, and this question does not assume that the files are large. And there is no concrete solution.
i have 39 csv files . I want to load this file by Java and set as one variable. Below paragraph is my coding which works for one file, but i don't know how to multiple file merge. I want to load 39 file in directory and put them into one 2d array.
public static String readCSV(File csvFile) {
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer();
try {
bufferedReader = new BufferedReader(new FileReader(csvFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
String temp = null;
while((temp = bufferedReader.readLine()) != null) {
stringBuffer.append(temp+","); // temp 에 저장되어있는 한 줄을 더한다.
}
System.out.println(stringBuffer);
} catch (IOException e) {
e.printStackTrace();
}
// -10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,,,,,,,,,,1,2,3,4,5,6,7,8,9,10, 반환
return stringBuffer.toString();
}
public static String[] parse(String str) {
String[] strArr = str.split(","); // 쉼표가 1개인 것을 기준으로 나누어서 배열에 저장
return strArr;
}
public static void main(String[] args) throws IOException {
//mergeCsvFiles("sample", 4, "D:\\sample_folder\\" + "merge_file" + ".csv");
String str = readCSV(new File("D:/sample_folder/sample1.csv"));
String[] strArr = parse(str); // String 배열에 차곡차곡 담겨서 나온다.
int varNumber = 45;
int rowNumber = strArr.length/varNumber;
String[][] Array2D = new String[varNumber][rowNumber];
for(int j=0;j<varNumber;j++)
{
for(int i=0; i<rowNumber;i++)
{
String k = strArr[i*varNumber+j];
Array2D[j][i]= k;
}
} //2D array 배열을 만들기
//String[][] naArray2D=removeNA(Array2D,rowNumber,varNumber); //NA 포함한 행 지우기
// /* 제대로 제거 됐는지 확인하는 코드
for(int i=0;i<varNumber;i++){
for(int j=0;j<16;j++){
System.out.println(Array2D[i][j]);
}
System.out.println("**********************NA제거&2차원 배열**********************");
}
// */
}
}
答案 0 :(得分:-1)
您的逻辑是正确的,对于多个文件,您可以将所有这些csv文件放在一个目录中,并使用java列表文件,您可以通过以下方式获取所有文件名
File txtPat = new File("C:\\Users\\Admin\\Desktop\\") ;
File allFile[] = txtPat.listFiles();
for(int i = 0 ;i<allFile.length;i++)
{
System.out.println(allFile[i]);
}