如何使用jsp / java从csv文件中的特定列读取数据? [英] How to read data from a specific column from csv file using jsp/java?

查看:297
本文介绍了如何使用jsp / java从csv文件中的特定列读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application I need to read a specific column of tab separated csv file using jsp. But I can read the data of full row not a specific column.

我需要帮助。请帮助我

感谢

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 数组,那么您可以获取特定列,但是您需要读取整行增益。

I don't think you can read specific column.Better to read entire row using CSVParser or you can read CSV line by line and split it and get String array then you can get specific column but yes you need to read whole row gain.

尝试一下。

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

方式你可以读取特定的列。

By this way you can read specific column.

这篇关于如何使用jsp / java从csv文件中的特定列读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆