メモ:ファイル->DTO(インスタンス動的生成)

/**
* ファイル -> DTOインスタンス動的生成).
*/
public List toBeans(String fileName, String className) throws Exception {
Class clazz = getClass().getClassLoader().loadClass(className);
List list = new ArrayList();
FileReader fr = null;
BufferedReader br = null;
try {
fr= new FileReader(fileName);
br = new BufferedReader(fr);
String line;
while *1 != null) {
Object instance = clazz.newInstance();
if (instance instanceof ***) {
((Xxx) instance).パースメソッド(line);
list.add((Xxx) instance);
} else {
throw new IllegalArgumentException(className
+ " は Xxx を実装したクラスではない。");
}
}
} finally {
if (br != null) try { br.close(); } catch (IOException e) {}
if (fr != null) try { fr.close(); } catch (IOException e) {}
}
return list;
}

*1:line = br.readLine(