简体   繁体   English

如何使用OpenCSV nextLine数组

[英]How to use the OpenCSV nextLine array

So, I'm using OpenCSV for a project I'm working on, and I've been doing fine so far, I managed to read a CSV file, as well as write to one. 因此,我正在将OpenCSV用于我正在从事的项目,到目前为止我做得很好,我设法读取了CSV文件并写入了一个文件。 Because it's my first time using OpenCSV, I decided to use a tutorial I found on the internet, and adapt it a bit. 因为这是我第一次使用OpenCSV,所以我决定使用我在Internet上找到的教程,并对其进行一些调整。 So, what I've done is made a method called ReadData(). 因此,我所做的就是创建一个称为ReadData()的方法。 I can read the CSV and print it to the console fine, but what I want to do is access the "nextLine" array outside of the method. 我可以读取CSV并将其正确打印到控制台,但是我想做的是在方法外部访问“ nextLine”数组。

    public void ReadData(String filelocation) {
    filelocation = System.getProperty("user.dir")+"/data/"+filelocation;
    try {
        CSVReader savereader = new CSVReader(new FileReader(filelocation), ',', '"', 0);
        String[] nextLine;
            while((nextLine = savereader.readNext()) != null) {
                if(nextLine != null) {
                    //Make sure everything went through
                    System.out.println("Data Read. Results:");
                    System.out.println(Arrays.toString(nextLine));
                }
            }
    } catch (FileNotFoundException ex) {
        //Handle Exception
    } catch (IOException ex) {
        //Handle Exception
    }
}

This method reads the data from a file I specify. 此方法从我指定的文件中读取数据。 But, I want to be able to do things with that data. 但是,我希望能够处理这些数据。 For example, I would want to take nextLine[0], and see if it is empty, if it is, do one thing. 例如,我要获取nextLine [0],看看它是否为空,如果是,则做一件事。 If it's not, then I want to do something else that would make it equal something. 如果不是,那么我想做其他事情,使它等于某些东西。

Thanks in advance, and I hope I was clear enough about what I'm trying to accomplish! 在此先感谢您,我希望我对要完成的工作足够清楚!

Regards, 问候,

Johnny 强尼

The problem you have is an java issue not opencsv. 您遇到的问题是Java问题,而不是opencsv。 The problem is that nextline is local to the method - in your case local to the try block. 问题是nextline对于方法来说是本地的-在您的情况下,对于try块而言是本地的。 So anything outside of the method cannot reference nextline. 因此,该方法之外的任何内容都无法引用nextline。

You could make nextline local to the method instead of the try and have the method return nextline (change from void to String[]) but you would only be returning the last line. 您可以将nextline设置为方法的局部方法,而不是try方法,并让该方法返回nextline(从void更改为String []),但只返回最后一行。

For what you want (to be able to check all the data after it has been read) is to store each String[] you read into a List and have the method return that. 对于您想要的(读取之后能够检查所有数据)是将您读取的每个String []存储到List中,并让该方法返回该值。 But if you are going to do that you should just use the readAll method of CSVReader ( http://opencsv.sourceforge.net/apidocs/index.html ). 但是,如果要执行此操作,则应仅使用CSVReader的readAll方法( http://opencsv.sourceforge.net/apidocs/index.html )。 Be warned though that if you do that you are risking an OutOfMemoryException when dealing with large files. 请注意,如果这样做会在处理大文件时冒OutOfMemoryException的风险。

Hope that helps. 希望能有所帮助。

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

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