How to read sdcard CSV file?

0

Currently I read a csv file from the assets folder with the following code:

    String arquivoCSV = "clientes.csv";
    AssetManager manager = context.getAssets();
    InputStream inputStream = null;

    try{
        inputStream = manager.open(arquivoCSV);
    }catch (IOException e){
        e.printStackTrace();
    }

    BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));

But I need to update this file constantly, and through the assets folder it is not possible, I need to read the sscard's CSV file, does anyone have any idea how to do it?

I used the code, but now it does not identify any lines, it follows the code:

    File path = Environment.getExternalStorageDirectory();
    File file = new File(path, "/produtos.csv");
    FileInputStream fileInputStream = new FileInputStream(file);

    BufferedReader buffer = new BufferedReader(new InputStreamReader(fileInputStream));
    String line = "";
    conn.beginTransaction();
    conn.delete("PRODUTOS", "1", null);
        try{

            while ((line = buffer.readLine())!= null){
                String[] colunas = line.split(",");
                    if (colunas.length != 6){
                        Log.d("CSVParder","Ignorando linhas ruins");
                        continue;
                    }
                ContentValues cv = new ContentValues();
                cv.put("COD_PRODUTO",colunas[0].trim());
                cv.put("NOME",colunas[1].trim());
                cv.put("DESCR",colunas[2].trim());
                cv.put("GRUPO",colunas[3].trim());
                cv.put("SUBGRUPO",colunas[4].trim());
                cv.put("MEDIDA",colunas[5].trim());

                conn.insert("PRODUTOS",null,cv);

            }

        }catch (IOException e){
            e.printStackTrace();
        }
    conn.setTransactionSuccessful();
    conn.endTransaction();
    Toast.makeText(context,"Produtos atualizados com sucesso!",Toast.LENGTH_LONG).show();
    
asked by anonymous 07.03.2016 / 15:10

2 answers

3

Hello, Just create a File type and then pass it to FileInputStream.

This way:

File path = Environment.getExternalStorageDirectory();
File file = new File(path, "path/clientes.cvs");
FileInputStream fileInputStream = new FileInputStream(file);
    
07.03.2016 / 15:19
1

It worked perfectly, my csv was corrupted. Thanks!

File path = Environment.getExternalStorageDirectory();
File file = new File(path, "/produtos.csv");
FileInputStream fileInputStream = new FileInputStream(file);

BufferedReader buffer = new BufferedReader(new InputStreamReader(fileInputStream));
String line = "";
conn.beginTransaction();
conn.delete("PRODUTOS", "1", null);
    try{

        while ((line = buffer.readLine())!= null){
            String[] colunas = line.split(",");
                if (colunas.length != 6){
                    Log.d("CSVParder","Ignorando linhas ruins");
                    continue;
                }
            ContentValues cv = new ContentValues();
            cv.put("COD_PRODUTO",colunas[0].trim());
            cv.put("NOME",colunas[1].trim());
            cv.put("DESCR",colunas[2].trim());
            cv.put("GRUPO",colunas[3].trim());
            cv.put("SUBGRUPO",colunas[4].trim());
            cv.put("MEDIDA",colunas[5].trim());

            conn.insert("PRODUTOS",null,cv);

        }

    }catch (IOException e){
        e.printStackTrace();
    }
conn.setTransactionSuccessful();
conn.endTransaction();
Toast.makeText(context,"Produtos atualizados com sucesso!",Toast.LENGTH_LONG).show();
    
07.03.2016 / 17:41