简体   繁体   English

如何使用jsp / java从csv文件的特定列读取数据?

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

In my application I need to read a specific column of tab separated csv file using jsp. 在我的应用程序中,我需要使用jsp读取制表符分隔的csv文件的特定列。 But I can read the data of full row not a specific column. 但是我可以读取全行而不是特定列的数据。

I need help this regard. 我需要这方面的帮助。 Please help me 请帮我

Thanks 谢谢

mycode: 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>

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. 我不认为您可以读取特定的列,最好使用CSVParser读取整行,或者可以逐行读取CSV并将其拆分并获取String数组,然后可以获得特定的列,但是是的,您需要读取整行的增益。

Try it. 试试吧。

    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. 通过这种方式,您可以阅读特定的列。

I had a similar problem in Objective C the other day, but this is how I solved it. 前几天在Objective C中我遇到了类似的问题,但这就是我解决的方法。

This method assumes you know the column number of the data you want. 此方法假定您知道所需数据的列号。 (IE if you want column 1 of 6) (即,如果您想要第1列,共6列)

Read all the rows into strings and append them into one. 将所有行读取为字符串,并将其追加为一个。

Data sample: (columns 1 to 6) 数据样本:(第1至6列)

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

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

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

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

Then you should get this: 然后,您应该得到以下信息:

String combined = 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 组合在一起的字符串= 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

Next you need to split the string into an array of all values. 接下来,您需要将字符串拆分为所有值的数组。

Use code somewhat like this: (written off the top of my head so may be off.) 使用类似以下的代码:(写在我的头顶上,所以可能会关闭。)

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

Now you should have something like this: 现在,您应该具有以下内容:

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

The last step is to loop through the entire array and modulo for whatever column you need: 最后一步是遍历整个数组并对所需的任何列取模:

//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]);

    }

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

Edit: 编辑:

You added code to your question. 您在问题中添加了代码。 Above I was saving them into memory. 在上面,我将它们保存到内存中。 You just loop through and print them out. 您只需遍历并打印出来。 In your while loop, split each line separately into an array and then either hardcode the column number or modulo the length of the array as the index. 在while循环中,将每一行分别拆分为一个数组,然后对列号进行硬编码或对数组的长度取模作为索引。

public class ParseCSVs { public static void main(String[] args) { 公共类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.

相关问题 如何从 java 中的 csv 文件中读取行的特定列 - How to read a specific column of a row from a csv file in java 如何使用java中的CsvReader从csv文件中读取特定行 - how to read a specific row from csv file using CsvReader in java 如何从 csv 文件中获取特定列并使用 java 将该列项放入 jComboBox? - How can i get specific column from csv file and put that items of column into jComboBox using java? 从Java中的特定csv列计算数据 - Compute data from a specific csv column in java 如何从csv文件读取特定数据并将其删除? - How to read specific data from a csv file and delete it? 使用Java从文件的特定列中随机读取文本 - Randomly read text from specific column in a file using Java 如何使用Java将基于列名而不是按列索引的特定列从一个CSV文件转换为另一个CSV - How To Transfer specific Columns based on column name not by column index From One CSV File Into Another CSV Using Java 如何使用apache common从CSV文件中读取列的值而不遍历Java中的行 - How to read values of a column from CSV file without traversing rows in java using apache common 如何使用Java中的jena将从csv读取的行数中的数据插入到rdf文件中? - How to insert data from numbers of rows read from csv into a rdf file using jena in Java? 如何从CSV文件中读取特定的列? - How to read specific columns from CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM