How to read sdcard CSV file?

Asked

Viewed 151 times

0

Currently I read a file csv of the briefcase assets 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));

However I need to update this file constantly, and by Assets folder is not possible, I need to read the sdcard’s CSV file, does anyone have any idea how to do?

I used the code, but now it does not identify any line, follow 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();
  • Try Change your while to this while ((line = buffer.readline())!= -1) { // input } I think it works

  • There is no way, String compare with int, actually I think q is not able to read any lines because in the log is giving too many Csvparder","Ignoring bad lines" and the comic is empty!

2 answers

3


Hello, Simply create a File type and then pass it to Fileinputstream

In this way:

File path = Environment.getExternalStorageDirectory();
File file = new File(path, "path/clientes.cvs");
FileInputStream fileInputStream = new FileInputStream(file);
  • I used the code, but now it does not identify any line, follow the code:

1

It worked perfectly, my csv that was corrupted. Thank you!

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();
  • If possible mark as answered to close the topic, and give a help ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.