사용자 정의 배열로로드하려는 .dat 파일이 있습니다. 배열에 데이터를 실제로로드하려면 어떻게해야합니까? 데이터는 (String, int, int, double, String)로 구성됩니다.다차원 배열로 데이터 파일 읽기 Java
class CDinventoryItem{
private CDinventoryItem [] inven = new CDinventoryItem[1000];
public CDinventoryItem (String title, int itemNumber, int numberofUnits,
double unitPrice, String genre){
DataInputStream input;
try{
input = new DataInputStream(new FileInputStream("inventory.dat"));
inven = input.read(CDinventoryItem[]); //line I am receiving error on
}
catch (IOException error){
JOptionPane.showMessageDialog(null, "File not found",
"" ,JOptionPane.ERROR_MESSAGE);
}
}
}
는 이제 ReadFile을가 ... 자신의 클래스에
class readFile {
public CDinventoryItem[] inven;
public readFile(){
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("inventory.dat"));
String line = null;
int i = 0;
while ((line = in.readLine()) != null) {
// process each line
String[] parts = line.split(",");
String title = parts[0];
int itemNumber = Integer.parseInt(parts[1]);
int numberofUnits = Integer.parseInt(parts[2]);
double unitPrice = Double.parseDouble(parts[3]);
String genre = parts[4];
CDinventoryItem item = new CDinventoryItem(title, itemNumber, numberofUnits,
unitPrice, genre);
//add item to array
inven[i] = item;
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}}
만 내 CDinventory 클래스
readFile invenItem = new readFile();
list = new JList(invenItem.inven);
에서 호출하고 있지만, 그것은 나에게 제공 : 스레드의 예외를 "main"java.lang.ArrayIndexOutOfBoundsException : 1 온라인 : readFile invenItem = new readFile();
그런 식으로 배열을 전달하지 않는 것 같습니다.
내가 질문을 이해하지 않습니다 여기에
당신이 시작하는 몇 가지 코드입니다. 'CDinventoryItem'의 1 차원 배열을 원하는 것처럼 들립니다. – Gabe죄송하지만 어쩌면 오해입니다. 방금 .dat의 내용을 가져 와서 내 CDinventoryItem 배열에로드하려고합니다. – user569127