简体   繁体   English

如何从CSV文件输出特定行的内容?

[英]How do I output the contents of a specific row from a CSV file?

The goal of my code is to ask the user for an input, and my code will search my CSV file and output the correct row and column the data lies. 我的代码的目标是要求用户输入,我的代码将搜索我的CSV文件并输出数据所在的正确行和列。 However, I'm quite stumped as to how to extract that specific cell so I can then edit the contents. 但是,我对于如何提取该特定单元格感到很困惑,因此可以编辑内容。

Say I search for banana and my code searches for banana and then outputs that specific row which will include banana,store to a variable so I can then edit the location, and put it back in the correct cell. 假设我搜索了banana ,我的代码搜索了banana ,然后输出包含banana,store特定行,并将其banana,store到变量中,这样我就可以编辑位置,并将其放回正确的单元格中。

Here's the code for finding the row and column of the specific text I search for: 这是用于查找我要搜索的特定文本的行和列的代码:

try{

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

//I now need to output the contents of the row into something so I can edit it

I'm now just needing to extract the row into something so I can then edit the location, but I'm clueless as to how I should be approaching this? 现在,我只需要将行提取到某些内容中,以便随后可以编辑位置,但是对于如何处理这个问题我一无所知。

If anyone has any tips or methods I should be following, then that would be greatly appreciated. 如果有人有我应该遵循的任何技巧或方法,那将不胜感激。

Thank you. 谢谢。

Java String s are immutable; Java String是不可变的; you can't change them, you have to create new ones. 您无法更改它们,必须创建新的。 List s and arrays, on the other hand are mutable (unless they're not). 另一方面, List和数组是可变的(除非它们不是可变的)。

You can do... 你可以做...

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);
            String myNewString = "something else";
            myEntries.get(row)[column] = myNewString;  // Replaces the String at this array index
        }
    }
}

// Now write the contents to a CSVWriter variable called 'myCSVWriter'
for (String[] entry : myEntries) {
    myCSVWriter.writeNext(entry);
}
myCSVWriter.close();

// Actually -- instead of that -- an easier way would be...
myCSVWriter.writeAll(myEntries);
myCSVWriter.close();

When you're done, you can use the myEntries collection to overwrite the contents of the file. 完成后,您可以使用myEntries集合覆盖文件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM