Java:解析由","分隔的CSV和新线

时间:2015-01-12 20:38:31

标签: java csv

所以我有一个CSV,其数据格式类似:

a,b,c,d
e,f,g,h
i,j,k,l

我可以用“,”解析它,但问题是当我到达d所在的位置时,它会解析为:“d e”

我如何解决这个问题,它可以看到d和e分开?有没有办法解析“,”或“新行”?

 Scanner scanner;
    try {
        scanner = new Scanner(new File("C:/Users/Desktop/orders.csv"));

        //Set the delimiter used in file
        scanner.useDelimiter(",");

        //Get all tokens and store them in some data structure
        //I am just printing them
        while (scanner.hasNext())
        {
            orders++;
            //System.out.print(scanner.next() + "\n");
            //System.out.println(orders);
            System.out.println("Order = " + scanner.next());
            scanner.next(); //Email
            scanner.next(); //Finacial
            scanner.next(); //Paid at
            scanner.next(); //Fulfillment status
            scanner.next(); //Fulfillment at
            scanner.next(); //Accept
            scanner.next(); //Currency
            scanner.next(); //Subtotal
            System.out.println("SHIPPING: " + scanner.next()); //Shipping
            scanner.next(); //Tax
            scanner.next(); //Total
            scanner.next(); //Discount Code
            scanner.next(); //Discount Amount
            scanner.next(); //Shipping Method
            scanner.next(); //Created at
            scanner.next(); //Lineitem Quantity
            scanner.next(); //Lineitem Name
            scanner.next(); //Lineitem Price
            scanner.next(); //Lineitem compare at price
            scanner.next(); //Lineitem SKU
            scanner.next(); //Lineitem requires shipping
            scanner.next(); //Lineitem Taxable
            scanner.next(); //Lineitem Fulfillment status
            scanner.next(); //Billing Name
            scanner.next(); //Billing Street
            scanner.next(); //Billing address1
            scanner.next(); //Billing address2
            scanner.next(); //Billing Company
            scanner.next(); //Billing City
            scanner.next(); //Billing Zip
            scanner.next(); //Billing providence
            scanner.next(); //Billing country
            scanner.next(); //Billing phone
            scanner.next(); //Shipping name
            scanner.next(); //Ship street
            scanner.next(); //Ship address1
            scanner.next(); //Ship address2
            System.out.println(scanner.next()); //Shipping company
            scanner.next(); //Shipping city
            scanner.next(); //Shipping zip
            scanner.next(); //Shipping providence
            scanner.next(); //Shipping country
            scanner.next(); //Shipping phone
            scanner.next(); //Notes
            System.out.println(scanner.next()); //Notse attributes
            scanner.next(); //TX State Tax
            scanner.next(); //Mckinney Municipal Tax
            System.out.println(scanner.next()); //Cancelled at
            scanner.next(); //Payment method
            System.out.println(scanner.next()); //Payment reference
            scanner.next(); //Refunded amount
            scanner.next(); //Vendor
            scanner.next(); //Id
            System.out.println(scanner.next()); //Tags
            scanner.next(); //Risk level
            scanner.next(); //Source
            System.out.println(scanner.next()); //Lineitem Discount + NEXT order PROBLEM IS HERE!!

            System.out.println("NEXT"); 
        }

        //Do not forget to close the scanner 
        scanner.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

还使用分隔符“\ n”,以便您的标记将在逗号和换行符上拆分。

答案 1 :(得分:0)

我建议您使用现有的解析器来简化您的工作 例如,使用opencsv,您可以定义自己的分隔符,引用字符...

如果您绝对想要自己编写代码,则必须首先使用扫描仪分隔线,然后分割每一行:

Scanner scanner = new Scanner(new File("C:/Users/Desktop/orders.csv"));
scanner.useDelimiter("\n");
while (scanner.hasNext()) {
    String[] values = scanner.next().split(",");
    System.out.println(values[0]); // Email
    // Etc.
}