【问题标题】:Reading in a data file (txt) into java variables将数据文件(txt)读入java变量
【发布时间】: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 的数组。

标签: java io


【解决方案1】:

这是一种方法。我使用line.split("\\|")| 作为分隔符来分割行。由于在 Summary 和 Total 之前的文件中有一个空行,您可以使用 if(line.isEmpty()) 打破循环。

    try {
        reader = new BufferedReader(new FileReader("resources/inventory.dat"));
        //ignore first two lines
        line = reader.readLine(); //read first line
        line = reader.readLine(); //read second line

        line = reader.readLine(); //read third line
        while (line != null){

            if(line.isEmpty())
                break;

            String [] l  = line.split("\\|"); //Split the lines

            //Add data
            itemID.add(l[0]);
            itemName.add(l[1]);
            itemDesc.add(l[2]);
            weight.add(l[3]);
            quant.add(Integer.parseInt(l[4].trim()));
            price.add(Float.parseFloat(l[5].trim()));
            aisle.add(l[6]);
            bin.add(l[7]);

            line = reader.readLine();

        }
    }

【讨论】:

  • 对于quant.add(Integer.parseInt(l[4].trim()));,它遇到了一个数字格式异常,因为它读入“144”,int 后面有一堆空格
  • @vnguyen56 .trim() 删除这些空格。它对我来说很好。您应该检查l[4] 是什么来查看问题。
猜你喜欢
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-17
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多