0

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.

I need help this regard. Please help me

Thanks

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>
2
  • Try parsing the full row you read. Commented Jun 13, 2012 at 10:46
  • I added my code with the question Commented Jun 13, 2012 at 10:52

3 Answers 3

2

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem in Objective C the other day, but this is how I solved it.

This method assumes you know the column number of the data you want. (I.E. if you want column 1 of 6)

Read all the rows into strings and append them into one.

Data sample: (columns 1 to 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

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

String 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

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.

Comments

0

public class ParseCSVs { public static 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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.