简体   繁体   English

在Java中读取和破坏CSV文件:

[英]Reading & Breaking CSV File in Java:

I am editing this question to be more specific and I've learned some Jave to find the solution to my problem. 我正在编辑此问题以使其更具体,并且我已经学会了一些Jave来找到问题的解决方案。 I have a file in CSV format like this: 我有一个CSV格式的文件,如下所示: 包含信息的CSV文件 or in excel like this: 或像这样在excel中: Excel中的相同CSV文件 Now I am using Java program to read the second line of file and separate each Comma Separated Value and write it to console as well as on other output file and it was done easily. 现在,我正在使用Java程序读取文件的第二行,并分隔每个逗号分隔值,并将其写入控制台以及其他输出文件,这很容易做到。 Now I'm trying to break the last value of: 现在,我试图打破最后一个值:

S/1,M/1,L/1,XL/1 | S / 1,M / 1,L / 1,XL / 1 | 2XL/1,3XL/1,4XL/1,5XL/1 | 2XL / 1,3XL / 1,4XL / 1,5XL / 1 | MT/1,LT/1 (Original) S/1,M/1,L/1,XL/1,2XL/1,3XL/1,4XL/1,5XL/1,MT/1,LT/1 (Modified using program to remove spaces and replacing the Pipes (|) with comma. MT / 1,LT / 1(原始)S / 1,M / 1,L / 1,XL / 1,2XL / 1,3XL / 1,4XL / 1,5XL / 1,MT / 1,LT / 1(使用程序进行了修改,以删除空间并用逗号替换Pipes(|)。

In each value, There is the size name before Forward Slash (/) and its quantity is after that. 在每个值中,正斜杠(/)之前都有尺寸名称,其后是数量。 What I'm trying is using the Forward Slash (/) to separate the size with its quantity. 我正在尝试使用正斜杠(/)将大小与其数量分开。 And the problem is that the size may contain the forward slash as well (eg 12/BT or 2BT/2x). 问题是大小也可能包含正斜杠(例如12 / BT或2BT / 2x)。 I've tried many algorithms like reversing the whole array or storing the slash count but not getting the success. 我尝试了很多算法,例如反转整个数组或存储斜杠计数,但没有成功。 The whole code to read file and break the comma separated values into separate columns of file is as following: 读取文件并将逗号分隔的值分成文件的单独列的整个代码如下:

import java.io.*;
import javax.swing.*;
public class ReadFile3c{
public static void main(String args[]){
    try{
        //Getting File Name
        String fileName = JOptionPane.showInputDialog("Enter File Name") + ".csv";
        //Creating Stream with File
        FileReader fr = new FileReader(fileName);
        //Applying Buffer Filter
        BufferedReader br = new BufferedReader(fr);


        //Reading First line then Second Line
        String s = br.readLine();
        s = br.readLine();
        s = s + ",";//adding comma at the end of the file
        s = s.replaceAll("\\s",""); //Eliminating Spaces
        s = s.replaceAll("\\|",",");    //Replacing Pipes with comma
        char charArray[] = s.toCharArray();

        //Declaring Strings and variablse for value separating function
        int n = 0;                              //Array Variable
        int m = 0;                              //Array Variable
        String[] inverted = new String[3];      //String to store inverted Commas Values
        String[] comma = new String[10];        //String to store comma Values
        String value = "";                      //Storing character values

        try{
            //Loop to cycle each character of file
            for(int j = 0; j<charArray.length;j++){

                //Inverted comma value separator
                if (charArray[j] == '"') {
                    j++;
                    //loop to gather values b/w invreted comma
                    while((charArray[j] != '"')){
                        value = value + charArray[j];
                        j++;
                    }
                    inverted[n] = value;
                    n++;
                    j++;
                    value = "";
                }else{ 
                    j = j - 1;
                    //comma Value separator
                    if (charArray[j] == ','){
                        j++;
                        //loop to gether values b/w commas
                        while((charArray[j] !=',')){
                            value = value + charArray[j];
                            j++;
                        }
                        comma[m] = value;
                        m++;
                        value = "";
                    }       
                }
            }
        }catch(Exception ex){
            System.out.println("in inner Exception Block" + ex);
        }

        //declaring variables to storing values
        String name, patternCode, placeSizeQty,width,length,utill,pArea,pPerimeter,totalPcs,placePcs,tSizes;
        name = inverted[0];
        patternCode = inverted[1];
        placeSizeQty = inverted[2];
        width = comma[0];
        length = comma[1];
        utill = comma[2];
        pArea = comma[3];
        pPerimeter = comma[4];
        totalPcs = comma[5];
        placePcs = comma[6];
        tSizes = comma[7];

        //printing all values on Console
        System.out.println("\nMarkerName: " + name);
        System.out.println("Width :" + width);
        System.out.println("Length :" + length);
        System.out.println("Utill :" + utill);
        System.out.println("Place Area :" + pArea);
        System.out.println("Place Perimeter :" + pPerimeter);       
        System.out.println("PatternCode: " + patternCode);
        System.out.println("PlaceSizeQty: " + placeSizeQty);
        System.out.println("Total Pcs :" + totalPcs);
        System.out.println("Place Pcs :" + placePcs);
        System.out.println("Total Sizes :" + tSizes);

        //Creating Output file
        String fileOutput = JOptionPane.showInputDialog("Enter Output File Name") + ".txt";
        //File Writer
        try{
            //Creating Stream with output file
            FileWriter fw = new FileWriter(fileOutput);
            //Applying Buffring Stream
            PrintWriter pw = new PrintWriter(fw);
            //Declaration
            String outputLine = null;
            //Writing Inverted inputs
            for (int u = 0; u <=2 ;u++ ) {
                outputLine = inverted[u];
                pw.println(outputLine);
                System.out.println("Writing: " + outputLine);
            }//end of for
            //writing comma inputs
            for (int t = 0;t <=7  ; t++ ) {
                outputLine = comma[t];
                pw.println(outputLine);
                System.out.println("Writing: " + outputLine);
            }//end of for

            pw.flush();
            pw.close();
            fw.close();
            fr.close();
            br.close();                 

        }catch(Exception ex){
            System.out.println("Output: " + ex);
        }//End of output catch

    }catch(IOException ex){
        System.out.println(ex);
    }//end of catch
}//end of catch
}//end of Class

And the code to Break the Size and quantity and store it in Double array (Not completed) is as Following: 破坏大小和数量并将其存储在Double array(未完成)中的代码如下:

import java.io.*;
import javax.swing.*;
public class ReadFileInvert{
public static void main(String args[]){
    try{
        String fileName = JOptionPane.showInputDialog("Enter File Name") + ".csv";
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
    String s = br.readLine();
        System.out.println(s);
        s = s.replaceAll("\\s","");
        s = s.replaceAll("\\|",",");
    System.out.println(s);  

        char charArray[] = s.toCharArray();
        char charArrayI[] = new char[charArray.length + 1]; 

        int j = 0;
        String value = "";

        for(int i = charArray.length; i > 0; i--){
            charArrayI[j] = charArray[i];
            value = value + charArrayI[j];
            j++;                
        }
        System.out.println("1" + value);

    }catch(Exception ex){
        System.out.println(ex);
    }
}
}

Now in simple I just want to Separate the sizes (Which may contains the Forward Slashes) with its quantity (After last slash of each value) and store it in double array Like charArray[sizeName][Qty]. 现在简单地讲,我只想用大小(每个值的最后一个斜杠之后)分隔大小(可能包含正斜杠)并将其存储在双数组中,如charArray [sizeName] [Qty]。 Sorry if i didn't explained my problem well as I'm Learning the Coding. 很抱歉,如果我在学习编码时没有很好地解释我的问题。 but I'll provide as much info as you want. 但我会根据需要提供尽可能多的信息。

Have you considered looking at the CAD software export to see if there is a solution on the file creation side? 您是否考虑过查看CAD软件导出,以查看文件创建方面是否有解决方案? Or is this file coming from a third party? 还是此文件来自第三方?

OK. 好。 So, after the hard work of whole day, I've found the following solution to my problem: 因此,经过一整天的辛苦工作,我发现了以下解决问题的方法:

import java.io.*;
import javax.swing.*;
public class ReadFileInvert2{
public static void main(String args[]){
    try{
        String fileName = JOptionPane.showInputDialog("Enter File Name") + ".csv";
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();
        System.out.println(s);
        s = s.replaceAll("\\s","");
        s = s.replaceAll("\\|",",");
        System.out.println(s);  
        char charArray[] = s.toCharArray(); 
        int x = charArray.length - 1;
        charArray[x] = ',';

        int no = 1;
        int size = 1;
        int qty = 2;
        String sizeS = "";
        String qtyS = "";
        //String resSet[][] = new String[4][2];
        String resSize[] = new String[20];
        String resQty[] = new String[20];
        int slashNo = 0;
        String value = "";


        for (int j = 1; j < charArray.length; j++){
            int n = j;
            if (charArray[j] == ','){
                j++;
            }
            while (charArray[j] != ','){
                if (charArray[j] == '/') {
                    slashNo = j;
                    //j++;
                }
                value = value + charArray[j];
                //System.out.println(value);
                j++;
            }   
            for (int k = n;k < slashNo; k++ ) {
                sizeS = sizeS + charArray[k];
                //System.out.println(sizeS);
            }
            for (int l = slashNo + 1; l < j; l++ ) {
                qtyS = qtyS + charArray[l];
                //System.out.println(qtyS);
            }
            resSize[no] = sizeS;
            System.out.println(resSize[no]);
            resQty[no] = qtyS;
            System.out.println(resQty[no]);

            System.out.println("Size is: " + resSize[no] + ", and Qty is: " + resQty[no]);

            no++;
            slashNo = 0;
            sizeS = "";
            qtyS = "";
        }
        String fileOutput = JOptionPane.showInputDialog("Enter Output File Name: ") + ".txt";
        try{
            FileWriter fw =  new FileWriter(fileOutput);
            PrintWriter pw = new PrintWriter(fw);
            String outputSize = null;
            String outputQty = null;

            for (int t = 1; t < no; t++) {
                outputSize = resSize[t];
                outputQty = resQty[t];
                pw.println(outputSize + " = " + outputQty);
                System.out.println("Writing: "+ outputSize + " = " + outputQty);
            }
            pw.flush();
            pw.close();
            fw.close();
            fr.close();
            br.close();
        }catch(Exception ex){
            System.out.println("Output " + ex);
        }
    }catch(Exception ex){
        System.out.println(ex);
    }   
}
}

Now its in Generic form but will improve it later. 现在是通用形式,但以后会改进。 But still its working fine. 但是仍然可以正常工作。 Thanks for your Help stack overflow Community. 感谢您的帮助堆栈溢出社区。

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

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