簡體   English   中英

如何使用jsp / java從csv文件的特定列讀取數據?

[英]How to read data from a specific column from csv file using jsp/java?

在我的應用程序中,我需要使用jsp讀取制表符分隔的csv文件的特定列。 但是我可以讀取全行而不是特定列的數據。

我需要這方面的幫助。 請幫我

謝謝

mycode:

    <%@ page import="java.io.*"%>
    <html>
    <body>
    <% 
    String fName = "c:\\csv\\myfile.csv";
    String thisLine; 
    int count=0; 
    FileInputStream fis = new FileInputStream(fName);
    DataInputStream myInput = new DataInputStream(fis);
    int i=0; 
    %>
    <table>
    <%
    while ((thisLine = myInput.readLine()) != null)
   {
    String strar[] = thisLine.split(",");
    for(int j=0;j<strar.length;j++)
    {
    if(i!=0)
    {
     out.print(" " +strar[j]+ " ");
    }
    else
    {
    out.print(" <b>" +strar[j]+ "</b> ");
    }
    }
    out.println("<br>");
    i++;
    } 
    %>
    </table>
    </body>
    </html>

我不認為您可以讀取特定的列,最好使用CSVParser讀取整行,或者可以逐行讀取CSV並將其拆分並獲取String數組,然后可以獲得特定的列,但是是的,您需要讀取整行的增益。

試試吧。

    String fName = "C:\\Amit\\abc.csv";
    String thisLine;
    int count = 0;
    FileInputStream fis = new FileInputStream(fName);
    DataInputStream myInput = new DataInputStream(fis);
    int i = 0;
    while ((thisLine = myInput.readLine()) != null) {
        String strar[] = thisLine.split(",");
            System.out.println(strar[3]);
                        // Here column 2
        }
    }

通過這種方式,您可以閱讀特定的列。

前幾天在Objective C中我遇到了類似的問題,但這就是我解決的方法。

此方法假定您知道所需數據的列號。 (即,如果您想要第1列,共6列)

將所有行讀取為字符串,並將其追加為一個。

數據樣本:(第1至6列)

1,2,3,4,5,6
13,45,63,29,10,8
11,62,5,20,13,2

字符串1 = 1,2,3,4,5,6

字串2 = 13,45,63,29,10,8

字串3 = 11,62,5,20,13,2

然后,您應該得到以下信息:

組合在一起的字符串= 1,2,3,4,5,6,13,45,63,29,10,8,11,62,5,20,13,2 //add in the missing "," when you concatenate strings

接下來,您需要將字符串拆分為所有值的數組。

使用類似以下的代碼:(寫在我的頭頂上,所以可能會關閉。)

String[] values = combined.split(",");  

現在,您應該具有以下內容:

Values = `"1", "2", "3", ... etc`

最后一步是遍歷整個數組並對所需的任何列取模:

//Remember that java numbers arrays starting with 0.
//The key here is that all remainder 0 items fall into the first column.  All remainder 1 items fall into the second column.  And so on.

    for(int i = 0; i < values.length(); i++)
    {
            //Column1 - Column6 -> array lists of size values.length/number of columns
            //In this case they need to be size values.length/6
        if(i % 6 == 0)
            column1.add(values[i]);
        else if(i % 6 == 1)
            column2.add(values[i]);
        else if(i % 6 == 2)
            column3.add(values[i]);
        else if(i % 6 == 3)
            column4.add(values[i]);
        else if(i % 6 == 4)
            column5.add(values[i]);
        else if(i % 6 == 5)
                column6.add(values[i]);

    }

~~~~~~~~~~~~~~~~

編輯:

您在問題中添加了代碼。 在上面,我將它們保存到內存中。 您只需遍歷並打印出來。 在while循環中,將每一行分別拆分為一個數組,然后對列號進行硬編碼或對數組的長度取模作為索引。

公共類ParseCSVs {公共靜態void main(String [] args){

    try {

        // csv file containing data
        String strFile = "./input//SIMNumbers.csv";

        String line = "";

        System.out.println("Enter line number to configure");
        Scanner sc = new Scanner(System.in);
        int lineNumber = sc.nextInt();

        BufferedReader br = new BufferedReader(new FileReader(strFile));
        if ((line = br.readLine()) != null) {

            String cvsSplitBy = ",";
            String blankCell = null;
            // use comma as separator
            String[] cols = line.split(cvsSplitBy);
            for (int i = 0; i < cols.length; i++)
                System.out.println("Coulmns = " + cols[i]);
            // System.exit(0);
        } else
            System.out.println("No data found in csv");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

暫無
暫無

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

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