【问题标题】:How would I extract a specific row in a .CSV file?如何提取 .CSV 文件中的特定行?
【发布时间】:2016-11-11 02:24:58
【问题描述】:

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

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

我的问题是我不确定要使用哪些代码/方法/类来执行此操作。

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

目前,我的代码仅说明是否找到它,但我不确定如何提取该特定行,我希望你们能帮助我。

这是我点击按钮的代码:

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

我已经尽了最大努力,但我现在对如何提取行感到困惑,我该怎么做呢?

感谢您的帮助。

【问题讨论】:

    标签: java arrays list csv opencsv


    【解决方案1】:

    您需要的不是嵌套循环,而是一个 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);
                }
        }
    }
    

    【讨论】:

    • 这不能回答我的问题。如何找出某个值在哪一行?在我的 .CSV 文件中,“香蕉”位于第三行。为了找出它在哪一行,我应该实现什么代码?
    • 难以置信,我对此投了反对票。感谢stackoverflow。对于你的问题,我更新了我的答案。只需要添加索引。
    • 太好了,这回答了我的部分问题。现在,将该行提取到某些内容中,以便我可以编辑它的随附内容。即banana,store。我只想编辑位置,以便最终得到banana,home
    • 对不起,回答否决的帖子很痛苦。自己做。没有冒犯。
    • 我没有投反对票,但没关系。感谢您的帮助。
    【解决方案2】:

    您可以通过简单地计算索引来实现。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-14
        • 2020-05-17
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        相关资源
        最近更新 更多