Java将数据文件读入多维数组
我有一个 .dat 文件,我想将其加载到自定义数组中。我如何让它实际将数据加载到数组中。数据由 (String, int, int, double, String) 组成。
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);
,但它给了我一个: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 在线的: readFile invenItem = new readFile();
似乎不喜欢我那样传递数组。
I have a .dat file that I want to load into a custom array. How do I get it to actually load the data into the array. The data consists of a (String, int, int, double, String).
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);
}
}
}
So now readFile is in its own class...
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();
}
}
}
}}
and I am calling it from my CDinventory class
readFile invenItem = new readFile();
list = new JList(invenItem.inven);
but it gives me a: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
on line:
readFile invenItem = new readFile();
Doesn't seem to like me passing the array that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要逐行读取文件。拆分
,
上的每一行并创建一个 CDInventoryItem。将项目添加到您的数组中。另请注意,此方法不应位于 CDInventoryItem 的构造函数中。您的
CDInventoryItem
类甚至不应该有一个CDInventoryItem
数组。所有这些都应该在一个单独的类中完成。下面是一些可以帮助您入门的代码:
You need to read the file line by line. Split each line on
,
and create a single CDInventoryItem. Add the item to your array.Also, note that this method should not be in the constructor of
CDInventoryItem
. YourCDInventoryItem
class should not even have an array ofCDInventoryItem
s. All this should be done in a separate class.Here is some code to get you started: