如何提取.CSV文件中的特定行?

时间:2016-11-11 02:24:59

标签: java arrays list csv opencsv

我的代码的目标是要求用户输入,用户指定此输入,我的代码是搜索并找出用户输入所在的行。

一旦找到,我将有代码将该行提取到某个字符串(字符串数组?),这样我就可以编辑它的一部分内容。

我的问题是,我不确定要使用哪些代码/方法/类。

我现在正在设置代码,以便它读取.CSV文件并将其输出到类型列表的String数组中。然后,我遍历数组并查明指定的用户输入是否位于文件中。

目前,我的代码只是说如果找到了,但我不确定如何提取该特定行,我希望你们可以帮助我。

点击按钮,这是我的代码:

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

我已经尽了最大努力,但我现在很难知道如何提取这一行,我该如何去做呢?

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您需要的不是嵌套循环,只是 Arrays.toString 方法。

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

答案 1 :(得分:0)

你可以通过简单地计算指数来实现这一点。

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

答案 2 :(得分:-1)

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