如何提取.CSV文件中的特定行? [英] How would I extract a specific row in a .CSV file?

查看:312
本文介绍了如何提取.CSV文件中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码的目标是要求用户输入,用户指定此输入,而我的代码是搜索并找出用户输入位于哪一行.

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.

我现在已经设置了代码,以便它读取.CSV文件并将其输出到类型为list的String数组中.然后,我遍历数组,找出指定的用户输入是否位于文件中.

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?

感谢您的帮助.

推荐答案

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

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

这篇关于如何提取.CSV文件中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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