我必須從不同的 CSV 檔案加載資料。目前,我有這個。它運行良好,但基本上我必須將不同檔案中的資料加載到不同的陣列中(一個是 Books,另一個是客戶帳戶)。我不想重復代碼。我想讓此代碼 (LoadingData) 可重復用于我必須上傳的每個 CSV 檔案。關于我如何解決這個問題的任何想法。這是我的“加載”類到目前為止的樣子。
public class LoadingData {
ArrayList<Book> books = new ArrayList<>();
//ArrayList<Reader> readers = new ArrayList<>();
public void Loading(String fileName){
try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
String currentLine;
while ((currentLine = br.readLine()) != null){
// separating the data by the comma
String[] detailed = currentLine.split(",");
// skiping the first line since it has only titles and no the data I want to store
if(detailed[0].equals("id"))
continue;
//Storing data in variables
String id = detailed[0];
String title = detailed[3];
String firstName = detailed[1];
String lastName = detailed [2];
String genre = detailed [4];
//adding the data to the arraylist
books.add(new Book(id, title, firstName, lastName, genre));
}
} catch (IOException ioe) {
ioe.printStackTrace();
}}
}
uj5u.com熱心網友回復:
public class LoadingData {
public LoadingData(ArrayList<Book> books, String fileName){
this.file = file;
this.books = books;
}
public ArrayList<Book> Loading(this.fileName){
try (BufferedReader br = new BufferedReader(new FileReader(fileName))){
String currentLine;
while ((currentLine = br.readLine()) != null){
// separating the data by the comma
String[] detailed = currentLine.split(",");
// skiping the first line since it has only titles and no the data I want to store
if(detailed[0].equals("id"))
continue;
//Storing data in variables
String id = detailed[0];
String title = detailed[3];
String firstName = detailed[1];
String lastName = detailed [2];
String genre = detailed [4];
//adding the data to the arraylist
books.add(new Book(id, title, firstName, lastName, genre));
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return books;
}
}
uj5u.com熱心網友回復:
我知道您需要一種loading方法來處理任何在檔案的每一行中包含未知數量欄位的 CSV 檔案。
下面的代碼使用反射來創建物件并將它們添加到通用List。
出于演示目的,我撰寫了一個類的最小實作Book和一個我命名為 .csv 的示例 CSV 檔案books.csv。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
public class LoadingData {
List<Object> list = new ArrayList<>();
public void loading(String fileName, Class<?> theClass) {
Constructor<?>[] ctors = theClass.getConstructors();
Constructor<?> ctor = ctors[0];
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String currentLine;
boolean first = true;
while ((currentLine = br.readLine()) != null){
String[] detailed = currentLine.split(",");
if (first) {
first = false;
continue;
}
Object instance = ctor.newInstance((Object[]) detailed);
list.add(instance);
}
}
catch (IllegalAccessException |
IllegalArgumentException |
InstantiationException |
InvocationTargetException |
IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
LoadingData ld = new LoadingData();
ld.loading("books.csv", Book.class);
System.out.println(ld.list);
}
}
class Book {
String id;
String title;
String firstName;
String lastName;
String genre;
public Book(String id, String title, String firstName, String lastName, String genre) {
this.id = id;
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
this.genre = genre;
}
public String toString() {
return String.format("%s by %s %s (%s)", title, firstName, lastName, genre);
}
}
這是檔案的內容 books.csv
ID,TITLE,FIRST NAME,LAST NAME,GENRE
1,Moby Dick,Herman,Melville,Adventure
2,To Kill a Mockingbird,Harper,Lee,Fiction
這是運行上述代碼時的輸出。
[Moby Dick by Herman Melville (Adventure), To Kill a Mockingbird by Harper Lee (Fiction)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376577.html
上一篇:讀取txt檔案錯誤地使用分隔符
