简体   繁体   English

如何从文本文件中读取并存储在java中的类对象中

[英]How to read and from a text file and store in a class object in java

I am trying to read and split data from a text file into a class object as an array. 我正在尝试从文本文件读取数据并将其拆分为数组的类对象。 I have a class ProductRecords: 我有一类ProductRecords:

class ProductRecord
{
    public String productCode;
    public String category;
    public String description;
    public int aisleNumber;
    public int currInventory;

    public ProductRecord(String code, String category, String description, int number, int currInventory)
    {
        this.productCode = code;
        this.category = category;
        this. description = description;
        this.aisleNumber = number;
        this.currInventory = currInventory;
    }

    public String getProductCode() {
        return productCode;
    }


    public String getCategory() {
        return category;
    }

    public String getDescription() {
        return description;
    }

    public int getAisleNumber() {
        return aisleNumber;
    }

    public int getCurrInventory() {
        return currInventory;
    }
}

and I am trying to read from this file 我试图从这个文件中读取

Thornton Hardware Store
7
P24-Qaa-1354 "hammer" "Bailey one-piece steel ripping hammer" 6 2
P24-Qbw-2495 "hammer drill" "Holsinger hammer drill, 8.5A 1/2-in" 7 1
P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2
P25-Taa-1244 "nail" "Yolen framing nails, 50-count 3-1/4-in 28-degree" 6 20
P25-Tab-3509 "nail" "Yolen finish nails, 1000-count 18-guage 2-in" 6 9
P25-Tab-3506 "nail" "Yolen finish nails, 1000-count 16-guage 2-1/2-in" 6 14
P25-Tac-3672 "nail" "Yolen roofing nails, coil 1-1/4-in" 8 5
LC "nail"
LC "plant"
LP P24-Qbw-2495
LP P62-Aaa-1387
S P25-Tab-3506 5
S P24-Qbw-2495 1
R P25-Tab-3509 10

The first information is the name of a store and the second is the total number of items in the inventory. 第一个信息是商店的名称,第二个信息是库存中的项目总数。 So for example on the third line to be stored in the class 因此,例如在要存储在类中的第三行上

P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2

P33-Qes-4782  --> productCode
"screwdriver" --> category
"Bailey 18-piece screwdriver set" --> description
5 --> aisle number
2 --> current inventory

when i tried splitting it, it splits the whitespace between the category and the description and I don't want that. 当我尝试拆分它时,它将拆分类别和描述之间的空格,我不希望这样。 Here is my code. 这是我的代码。

is there a way to split this without affecting the white spaces in the braces with scanner? 有没有办法在不影响扫描仪牙套空白的情况下进行拆分?

Scanner input = new Scanner(System.in);

System.out.println("Enter the input fileName: ");
String fileName = input.nextLine();

try {
    Scanner file = new Scanner(new File(fileName));

    System.out.println();

    //number of products in the inventory
    String inLine = file.nextLine(); // stores the name of the store
    String line = file.nextLine();
    int numberOfItems = Integer.parseInt(line);

    System.out.println(numberOfItems);

    System.out.println("*************************");
    while (file.hasNextLine()) {

        String[] tokens = file.nextLine().split(" ");
        //String[] tokens = file.nextLine().
        for (int i = 0; i < 5; i++) {
            System.out.println(tokens[i]);
            //System.out.println("\n");
        }
        System.out.println("\n");
    }

    /*ProductRecord[] records = new ProductRecord[file.nextInt()];
    records[0].productCode = file.next();
    System.out.println(records[0]);*/
    /* for(int i =0; i < numberOfItems;i++)
     {

         //records[i] = new ProductRecord([])
     }*/

} catch (IOException ioe) {
    System.out.println("file not found");
}

By using String[] tokens = file.nextLine().split("\\"") instead of String[] tokens = file.nextLine().split(" ") , it will successfully remove " " , as well as maintain the structure of the string enclosed. 通过使用String[] tokens = file.nextLine().split("\\"")而不是String[] tokens = file.nextLine().split(" ") ,它将成功删除" "并进行维护封闭的字符串的结构。

However, changes will need to be made to your logic to account for this change, as the debugging statements lose their current formatting by using this method. 但是,由于调试语句会丢失使用此方法的当前格式,因此需要对逻辑进行更改以解决此更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM