OpenCSV使用CsvToBeanBuilder(FileReader())时出错。

huangapple go评论83阅读模式
英文:

OpenCSV error using CsvToBeanBuilder(FileReader())

问题

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.opencsv.bean.CsvToBeanBuilder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        List<Restrictions> restrictionData = new CsvToBeanBuilder(new FileReader("restrictions.csv"))
            .withType(Restrictions.class).build().parse();
    }
}

错误信息:

C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:34: 错误: 找不到符号
        List<Restrictions> restrictionData = new CsvToBeanBuilder(FileReader("restrictions.csv"))
                                                                      ^
  符号:   方法 FileReader(String)
  位置: 类型 MainActivity
C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:35: 错误: 找不到符号
                    .withType(Restrictions.class).build.parse();
                                                 ^
  符号:   变量 build
  位置: 类 CsvToBeanBuilder

在您的 build.gradle 中确保已经正确导入了 OpenCSV 和 beanutils 库,同时确认 restrictions.csv 文件位于 app 模块的 src 文件夹下。您的 Restrictions 类应该按照 OpenCSV 的 bean 设置进行配置,如上述代码所示。如有帮助,谢谢!

英文:

I am using OpenCSV in an Android Java project with IntelliJ.

I am trying to run the following code to create a bean inside my project. Note the segment surrounded by "** **"

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.opencsv.bean.CsvToBeanBuilder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);


            **List&lt;Restrictions&gt; restrictionData = new CsvToBeanBuilder(FileReader(&quot;restrictions.csv&quot;))
                    .withType(Restrictions.class).build.parse();**
    }
}

I have taken this code from OpenCSV's site where they say:

> Here we simply name the fields identically to the header names. After
> that, reading is a simple job:

> List<Visitors> beans = new CsvToBeanBuilder(FileReader("yourfile.csv"))
> .withType(Visitors.class).build().parse();

>This will give you a list of the two beans as defined in the example input file. Note how type conversions to basic data types (wrapped and unwrapped primitives, enumerations, and Strings) occur automatically.

My code resembles their same style. Here is the error I get:

C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:34: error: cannot find symbol
            List&lt;Restrictions&gt; restrictionData = new CsvToBeanBuilder(FileReader(&quot;restrictions.csv&quot;))
                                                                      ^
  symbol:   method FileReader(String)
  location: class MainActivity

I have tried putting 'new' in front of FileReader, and when I do this I get THIS error:

C:\Programming\Android\GoTimeJavaVersion\app\src\main\java\com\example\gotimejavaversion\MainActivity.java:35: error: cannot find symbol
                    .withType(Restrictions.class).build.parse();
                                                 ^
  symbol:   variable build
  location: class CsvToBeanBuilder

I have OpenCSV and beanutils in my library, both implemented in my build.gradle, and my file restrictions.csv is in my app's src folder. My Restrictions class also has no errors and follows OpenCSV's bean setup to a tee.

What am I doing wrong? Any help appreciated, thank you!

答案1

得分: 1

使用以下任何构造函数来创建CsvToBeanBuilder。您正在使用的构造函数无效:-

CsvToBeanBuilder(CSVReader csvReader)
CsvToBeanBuilder(Reader reader)

示例:-

CSVReader csvReader = new CSVReader(new FileReader("restrictions.csv"));
List<Restrictions> restrictionData = new CsvToBeanBuilder(csvReader)
                .withType(Restrictions.class)
                .build()
                .parse();
英文:

Use any of the below constructors for CsvToBeanBuilder. Constructor you are using is not valid :-

CsvToBeanBuilder(CSVReader csvReader)
CsvToBeanBuilder(Reader reader)

example :-

CSVReader csvReader = new CSVReader(new FileReader(&quot;restrictions.csv&quot;));
List&lt;Restrictions&gt; restrictionData = new CsvToBeanBuilder(csvReader)
                .withType(Restrictions.class)
                .build()
                .parse();

huangapple
  • 本文由 发表于 2020年10月17日 02:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定