簡體   English   中英

在Java中讀取和破壞CSV文件:

[英]Reading & Breaking CSV File in Java:

我正在編輯此問題以使其更具體,並且我已經學會了一些Jave來找到問題的解決方案。 我有一個CSV格式的文件,如下所示: 包含信息的CSV文件 或像這樣在excel中: Excel中的相同CSV文件 現在,我正在使用Java程序讀取文件的第二行,並分隔每個逗號分隔值,並將其寫入控制台以及其他輸出文件,這很容易做到。 現在,我試圖打破最后一個值:

S / 1,M / 1,L / 1,XL / 1 | 2XL / 1,3XL / 1,4XL / 1,5XL / 1 | 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(|)。

在每個值中,正斜杠(/)之前都有尺寸名稱,其后是數量。 我正在嘗試使用正斜杠(/)將大小與其數量分開。 問題是大小也可能包含正斜杠(例如12 / BT或2BT / 2x)。 我嘗試了很多算法,例如反轉整個數組或存儲斜杠計數,但沒有成功。 讀取文件並將逗號分隔的值分成文件的單獨列的整個代碼如下:

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

破壞大小和數量並將其存儲在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);
    }
}
}

現在簡單地講,我只想用大小(每個值的最后一個斜杠之后)分隔大小(可能包含正斜杠)並將其存儲在雙數組中,如charArray [sizeName] [Qty]。 很抱歉,如果我在學習編碼時沒有很好地解釋我的問題。 但我會根據需要提供盡可能多的信息。

您是否考慮過查看CAD軟件導出,以查看文件創建方面是否有解決方案? 還是此文件來自第三方?

好。 因此,經過一整天的辛苦工作,我發現了以下解決問題的方法:

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);
    }   
}
}

現在是通用形式,但以后會改進。 但是仍然可以正常工作。 感謝您的幫助堆棧溢出社區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM