Java错误:找不到适合文件的构造函数

时间:2019-05-05 13:23:54

标签: java android java.util.scanner

我想让扫描仪在按下按钮时从Assets文件夹中的.txt文件中随机产生一行,但这会给我错误:

no suitable constructor found for File(InputStream)
constructor File.File(String) is not applicable
(argument mismatch; InputStream cannot be converted to String)
constructor File.File(URI) is not applicable
(argument mismatch; InputStream cannot be converted to URI) 

我尝试使用Inputstream,但是没有用。

编辑:获取随机文本行的文件名为“ scramble.txt”,并且它已经在资源文件夹中。

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

        File file = new File (getAssets().open("scramble.txt"));

        final Scanner sc = new Scanner(file);

        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //change the scramble length
                textView.setText(sc.nextLine());
            }
        });`

1 个答案:

答案 0 :(得分:0)

替换:

File file = new File (getAssets().open("scramble.txt"));
final Scanner sc = new Scanner(file);

具有:

final Scanner sc = new Scanner(getAssets().open("scramble.txt"));