无法在Android AsyncTask类的数组类型File []上调用listFiles()

时间:2014-09-05 04:19:57

标签: android file android-asynctask

我想请求一些关于我的代码中的错误的帮助。 在我的程序中,有一个使用AsyncTask的进度对话框,File类将传递到doInBackground

在该行找到错误 - File[] csvfile = csvLocation.listFiles();

  

无法在数组类型File []

上调用listFiles()

我的代码:

    @Override
    protected String doInBackground(File... csvLocation) {
        int count;
        long debugRV;


        File[] csvfile = csvLocation.listFiles();

        try{
            //Put all name of csv into a string array
            for(int j=0;j<20;j++){

                InputStreamReader isr = new InputStreamReader(new FileInputStream(csvfile[j]),"BIG5");
                BufferedReader buffer = new BufferedReader(isr);
                ContentValues contentValues=new ContentValues();
                String line = buffer.readLine();            //read first line to get the column
                String[] cols = line.split("\t");           

                while ((line = buffer.readLine()) != null) {
                    Log.d("ice","Read next record");
                    Log.d("ice","Line"+ Integer.toString(j)+": "+ line);
                    String[] str = line.split("\t");            //read every single line of record in csv
                    for (int i = 0; i < cols.length; i++) {
                        str[i] = str[i].replaceAll("\"", "");
                        contentValues.put(cols[i], str[i].trim());
                        Log.d("ice",cols[i] + "= " + str[i]);
                    }
                    debugRV = db.insert(tableName[j], null, contentValues);
                    Log.d("ice","Import result for " + tableName[j] +"= ");
                    Log.d("ice", Long.toString(debugRV));


                }
                buffer.close();

            }


        }catch (IOException e){
            Log.e(getClass().getSimpleName(), "Caught IOException when importing files", e);
        }

        return null;

    }

2 个答案:

答案 0 :(得分:1)

更改此

 File[] csvfile = csvLocation.listFiles();

File[] csvfile = csvLocation[0].listFiles();

答案 1 :(得分:0)

        for ur example don't need File[] csvfile = csvLocation.listFiles();  line u can use as below,



        try{
                    //Put all name of csv into a string array
                    for(int j=0;j<csvLocation.length;j++){

                        InputStreamReader isr = new InputStreamReader(new FileInputStream(csvLocation[j]),"BIG5");
                        BufferedReader buffer = new BufferedReader(isr);
                        ContentValues contentValues=new ContentValues();
                        String line = buffer.readLine();            //read first line to get the column
                        String[] cols = line.split("\t");           

                        while ((line = buffer.readLine()) != null) {
                            Log.d("ice","Read next record");
                            Log.d("ice","Line"+ Integer.toString(j)+": "+ line);
                            String[] str = line.split("\t");            //read every single line of record in csv
                            for (int i = 0; i < cols.length; i++) {
                                str[i] = str[i].replaceAll("\"", "");
                                contentValues.put(cols[i], str[i].trim());
                                Log.d("ice",cols[i] + "= " + str[i]);
                            }
                            debugRV = db.insert(tableName[j], null, contentValues);
                            Log.d("ice","Import result for " + tableName[j] +"= ");
                            Log.d("ice", Long.toString(debugRV));


                        }
                        buffer.close();

                    }


                }catch (IOException e){
                    Log.e(getClass().getSimpleName(), "Caught IOException when importing files", e);
                }



    or if u want to use listFiles() . try like this,



     @Override
        protected String doInBackground(Void... params) {
            int count;
            long debugRV;

    File dir = new File("[ur dir name]");
            File[] csvfile = dir.listFiles();

            try{
                //Put all name of csv into a string array
              for (File fdir : dir.listFiles()) {

                    InputStreamReader isr = new InputStreamReader(new FileInputStream(fdir ,"BIG5");
                    BufferedReader buffer = new BufferedReader(isr);
                    ContentValues contentValues=new ContentValues();
                    String line = buffer.readLine();            //read first line to get the column
                    String[] cols = line.split("\t");           

                    while ((line = buffer.readLine()) != null) {
                        Log.d("ice","Read next record");
                        Log.d("ice","Line"+ Integer.toString(j)+": "+ line);
                        String[] str = line.split("\t");            //read every single line of record in csv
                        for (int i = 0; i < cols.length; i++) {
                            str[i] = str[i].replaceAll("\"", "");
                            contentValues.put(cols[i], str[i].trim());
                            Log.d("ice",cols[i] + "= " + str[i]);
                        }
                        debugRV = db.insert(tableName[j], null, contentValues);
                        Log.d("ice","Import result for " + tableName[j] +"= ");
                        Log.d("ice", Long.toString(debugRV));


                    }
                    buffer.close();

                }


            }catch (IOException e){
                Log.e(getClass().getSimpleName(), "Caught IOException when importing files", e);
            }

            return null;

        }


Note: if u use  second approach don't pass file[] to asynctask.