Read a CSV file

Asked

Viewed 900 times

0

I was able to import the csv inside the cell, however when trying to read the file so that the application does the information inside csv it happens an error:

java.io.Filenotfoundexception: mesa.csv: open failed: ENOENT (No such file or directory)

probably because the file is not saved in the directory that the code is looking for, let’s go to the codes:

package realsysten.com.br.sigarestaurante;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Vitor on 16/06/2016.
 */
public class ImportarBancoActivity extends AppCompatActivity {

    Spinner spTabela;
    ArrayList<String> arquivosCelular = new ArrayList<String>();
    //ProgressDialog dialog;
    int op;
    BancoController crud;
    FTPController ftp;
    String nome;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.importar_banco);

        spTabela = (Spinner) findViewById(R.id.spTabela);

        chamaTabela();

        Button b3 = (Button) findViewById(R.id.btnInserir);
        Button b4 = (Button) findViewById(R.id.btnTabCancelar);

        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                nome = spTabela.getSelectedItem().toString();
                crud = new BancoController(getBaseContext());
                ftp = new FTPController();

                if (nome.equals("mesa.csv")) {
                    op = 1;
                } else if (nome.equals("produto.csv")) {
                    op = 2;
                } else {
                    op = 3;
                }

                try {
                    FileReader arq = new FileReader(nome);                          
                    BufferedReader lerArq = new BufferedReader(arq);
                    String linha = lerArq.readLine();
                    switch (op) {
                        case 1:
                            while (linha != null) {
                                String[] dados = linha.split(";");
                                String codigo = dados[0];
                                String descricao = dados[1];
                                String unitario = dados[2];

                                crud.insereProdutos(codigo, descricao, unitario);
                                linha = lerArq.readLine();
                            }
                            break;
                        case 2:
                            while (linha != null) {
                                String[] dados = linha.split(";");
                                String codigo = dados[0];
                                String descricao = dados[1];
                                String situacao = dados[2];

                                crud.insereControle(codigo, descricao, situacao);
                                linha = lerArq.readLine();

                            }
                            break;
                        case 3:
                            Toast.makeText(getApplicationContext(), "O nome do arquivo texto" +
                                    "não esta escrito da forma certa", Toast.LENGTH_LONG).show();
                            break;
                    }

                    arq.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ImportarBancoActivity.this, OpcoesActivity.class);
                startActivity(i);
                finish();
            }
        });
    }

    private void chamaTabela() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                listarTabela();
            }
        }).start();
    }

    private void listarTabela() {
        File diretorio = new File(Environment.getExternalStorageDirectory() + "/Import");
        File[] arquivos = diretorio.listFiles();

        if (arquivos != null) {
            int lenght = arquivos.length;

            for (int i = 0; i < lenght; ++i) {
                File f = arquivos[i];
                if (f.isFile()) {
                    arquivosCelular.add(f.getName());
                }
            }

            final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, arquivosCelular);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    spTabela.setAdapter(arrayAdapter);
                }
            });
        }
    }
}

The occore error here: FileReader arq = new FileReader(nome);

i think this command is searching in the wrong place the.csv table file, because, this file is saved in my sd. has some way to make the app search on: Environment.getExternalStorageDirectory()?

thanks since you

  • 2

    The error already tells the problem, the file was not found in the directory that was reported for the class File.

  • @diegofm yes, but what is the directory that class Filereader are searching? , have any default directory?

  • 1

    It is usually /sdcard/ that is returned by this method. I just don’t know if anything has changed in the latest versions.

  • @diegofm but if I want to change the directory have as?

  • I didn’t understand, change the directory returned by getExternalStorageDirectory?

  • @diegofm I pull the FTP file and store it inside the external memory and inside this external memory has a folder called Import and the file is inside it

Show 1 more comment

1 answer

2


I was able to solve, for anyone with the same problem in the future I did it as follows, I put this:

File path = Environment.getExternalStorageDirectory();
File file = new File(path, "/Import/" + nome);
FileInputStream inStream = new FileInputStream(file);
BufferedReader lerArq = new BufferedReader(new InputStreamReader(inStream));

instead of this:

FileReader arq = new FileReader(nome);                          
BufferedReader lerArq = new BufferedReader(arq);

Browser other questions tagged

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