How would I extract a specific row in a .CSV file?

你离开我真会死。 提交于 2020-03-26 16:42:50

问题


The goal of my code is to ask for a user input, the user specifies this input and my code is to search and find out which row the user input is located.

Once located, I will then have code to extract that row into something (string array?) so I can then edit part of it's contents.

My problem is that I'm not sure which code/methods/classes to use in order to do this.

I have my code set up right now so that it reads the .CSV file and outputs it into a String array of type list. I then iterate through the array and find out if the user input that was specified is located within the file.

At the moment, my code only says if it is found, but I am unsure as to how to extract that specific row, and I was hoping you guys could help me out.

Here's my code at the click of a button:

try{

        String strSearch = searchSerialField.getText();

        CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
        List<String[]> myEntries = reader.readAll();
        reader.close();

        //Write to existing file
        //CSVWriter writer = new CSVWriter(new FileWriter("test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER, "\r\n");

        //Iterate through my array to find the row the user input is located on
        for (String[] line : myEntries)
        {
            ArrayList<String> newLine = new ArrayList<String>();
            for (String word : line)
            {
                if (word.contains(strSearch))
                {
                    //Need a method so I can find out what row my item is on
                    System.out.println("Found - Your item is on row: ...");

                    //Code to extract row into something (String array?) so I can edit it's contents

                    //I then need to write the edited row back to it's original row

                }else
                {
                    System.out.println("Not found");
                }
            }
        }

   }catch (IOException e)
        {
            System.out.println(e);
        }

I've tried my best but I'm now stumped as to how I'm meant to extract the row, how can I go about doing this?

Thanks for the help.


回答1:


What you need is just not a nested loop, just a Arrays.toString method.

import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.opencsv.CSVReader;


public class Test {
    public static void main(String[] arg){
        search();
    }
    public static void search(){
        try{

            String strSearch = "banana";

            CSVReader reader = new CSVReader(new FileReader("d:\\textfile.txt"), ',');
            List<String[]> myEntries = reader.readAll();
            System.out.println(myEntries.size());
            reader.close();

            //Write to existing file
            //CSVWriter writer = new CSVWriter(new FileWriter("test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER, "\r\n");

            //Iterate through my array to find the row the user input is located on
            int i=1;
            for (String[] line : myEntries)
            {

                String textLine = Arrays.toString(line).replaceAll("\\[|\\]","");
                System.out.println(textLine);
                  if (textLine.contains(strSearch))
                    {
                        //Need a method so I can find out what row my item is on
                        System.out.println("Found - Your item is on row: ...:"+i);

                        //Code to extract row into something (String array?) so I can edit it's contents

                        //I then need to write the edited row back to it's original row

                    }else
                    {
                        System.out.println("Not found");
                    }
                  i++;
            }

       }catch (IOException e)
            {
                System.out.println(e);
            }
    }
}



回答2:


you can achieve that by simply counting the indices.

    String strSearch = searchSerialField.getText();
    CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
    List<String[]> myEntries = reader.readAll();
    reader.close();

    for (int row = 0; row<myEntries.size(); row++){
        for (int column = 0; column< myEntries.get(row).length; column++ ){
            if(myEntries.get(row)[column].trim().equalsIgnoreCase(strSearch)){
                System.out.println("found - your item is on row " +row + ", column "+ column);
            }              
        }
    }



回答3:


public static String[] getSpecificRowFromCsv(String filePath, int rowNum) {
    List<String[]> fileData = null;
    CSVReader reader = null;
    try {
        reader = new CSVReader(new FileReader(filePath));
        fileData = reader.readAll();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }             

    return fileData.get(rowNum - 1);
}


来源:https://stackoverflow.com/questions/40540211/how-would-i-extract-a-specific-row-in-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!