【发布时间】:2018-10-31 17:45:39
【问题描述】:
我有一个包含以下内容的数据文件“inventory.dat”:
Item ID |Item Name |Item Description |Weight |Quantity |Price |Isle |Bin
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BK1012923 |#8 x 1" Wood Screws (Qty 8) |#8 x 1" Wood Screws (Qty 8) - Plastic bag |0.04 |144 |0.65 |Isle-N23 |Bin-N23-14-3
BK1022344 |#8 x 1" Wood Screws (Qty 8) |#8 x 1 1/2" Wood Screws (Qty 8) - Plastic bag |0.06 |144 |0.65 |Isle-N23 |Bin-N23-14-3
BK1022344 |#8 x 1" Wood Screws (Qty 8) |#8 x 1 1/2" Wood Screws (Qty 8) - Plastic bag |0.06 |50 |0.65 |Isle-S18 |Bin-S18-01-2
Summary: 338 items Total Value: $219.70
我希望能够将每条数据读入单独的变量中,分别是商品 ID、商品名称、商品描述、重量、数量、价格、岛屿和垃圾箱。我将如何使用BufferedReader 执行此操作?
应忽略汇总和总值。
import java.io.*;
import java.util.*;
/**
*
*
*/
public class FileRead {
public static void main (String[] argv) {
BufferedReader reader;
String line;
String data;
ArrayList<String> itemID = new ArrayList<String>();
ArrayList<String> itemName = new ArrayList<String>();
ArrayList<String> itemDesc = new ArrayList<String>();
ArrayList<String> weight = new ArrayList<String>();
ArrayList<Integer> quant = new ArrayList<Integer>();
ArrayList<Float> price = new ArrayList<Float>();
ArrayList<String> aisle = new ArrayList<String>();
ArrayList<String> bin = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader("resources/inventory.dat"));
while ((line = reader.readLine() != null)) {
//
}
}
catch (IOException e)
{
System.err.println(e);
}
}
}
【问题讨论】:
-
你可以通过读取文件并解析数据来获得你想要的东西。到目前为止,您尝试过什么?
-
请将您尝试读取的代码贴在文件中,我们可以帮助解决具体问题。
-
刚刚发布。只是不确定在while循环中做什么
-
为了帮助您开始,
line.split("|")将在每个 |并给你一个Strings 的数组。