【问题标题】:How to read data from a specific column from csv file using jsp/java?如何使用jsp/java从csv文件的特定列中读取数据?
【发布时间】:2012-06-13 10:41:43
【问题描述】:

在我的应用程序中,我需要使用 jsp 读取制表符分隔的 csv 文件的特定列。但我可以读取整行而不是特定列的数据。

我需要这方面的帮助。请帮帮我

谢谢

我的代码:

    <%@ 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>

【问题讨论】:

  • 尝试解析您阅读的整行。
  • 我在代码中添加了问题

标签: java jsp csv


【解决方案1】:

我认为您无法读取特定列。最好使用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
        }
    }

通过这种方式,您可以阅读特定的列。

【讨论】:

    【解决方案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 循环中,将每一行分别拆分为一个数组,然后硬编码列号或将数组的长度取模作为索引。

    【讨论】:

      【解决方案3】:

      公共类 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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-28
        • 1970-01-01
        • 1970-01-01
        • 2021-09-17
        • 2013-05-06
        • 2020-03-11
        相关资源
        最近更新 更多