繁体   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