我有 CSV 文件,其中包含以下形式的示例数据:
220 30 255 0 0 Javascript
200 20 0 255 128 Thinking in java
,其中第一列是高度,第二列是厚度,接下来的三列是颜色的 rgb 值,最后一列是标题。所有这些都需要被视为单独的变量。我已经为此编写了自己的解决方案,但我想知道是否没有更好/更简单/更短的方法来做到这一点。然后提取的数据将用于创建 Book 对象,将每本书放入书籍数组并使用 swing 打印。这是代码:
private static Book[] addBook(Book b, Book[] bookTab){
Book[] tmp = bookTab;
bookTab = new Book[tmp.length+1];
for(int i = 0; i < tmp.length; i++){
bookTab[i] = tmp[i];
}
bookTab[tmp.length] = b;
return bookTab;
}
public static void main(String[] args) {
Book[] books = new Book[0];
try {
BufferedReader file = new BufferedReader(new FileReader("K:\\books.txt"));
String s;
while ((s = file.readLine()) != null) {
int hei, thick, R, G, B;
String tit;
hei = Integer.parseInt(s.substring(0, 3).replaceAll(" ", ""));
thick = Integer.parseInt(s.substring(4, 6).replaceAll(" ", ""));
R = Integer.parseInt(s.substring(10, 13).replaceAll(" ", ""));
G = Integer.parseInt(s.substring(14, 17).replaceAll(" ", ""));
B = Integer.parseInt(s.substring(18, 21).replaceAll(" ", ""));
tit = s.substring(26);
System.out.println(tyt+wys+grb+R+G+B);
books = addBook(new Book(wys, grb, R, G, B, tyt),books);
}
file.close();
} catch (IOException e) {
//do nothing
}
}