Copy file content in assets directory to existing Android file

1

I have a txt file with several lines in the assets directory and every time I start my application, I check if a file called database exists, if it exists, nothing is done, if it does not exist (first run), I wanted copy what you have in the txt that is in the assets directory for this new file

in my main class

protected void onCreate(Bundle savedInstanceState) {

final File file = new File(getFilesDir().getPath() + "/bd.txt"); // crio arquivo bd
    if (!file.exists()) {
        try {
            fileWriter = new FileWriter(file, true);
            Dados d = new Dados(); // instancio a classe que abre o arquivo no assets
           String data = d.Listar();
            fileWriter.append(processos);
            fileWriter.flush();

        } catch (Exception e) {


        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (Exception e) {
                }
            }
        }
    } else { // JA EXISTE
        System.out.println("arquivo ja existe");
    }
    }

in my class that manages what you have in assets

public class Dados extends Activity {

AssetManager assetManager = getResources().getAssets();
InputStream inputStream;
List<String> linhas_do_arquivo;

public String Listar () {

    try {
        inputStream = assetManager.open("arquivo.txt");//conteudo do que no diretorio assets
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String recebe_string = "";

        while ((recebe_string = bufferedReader.readLine()) != null) {

        // no caso eu faria um return recebe_string?

        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    //e aqui eu retornaria o que?
}

}

    
asked by anonymous 19.10.2015 / 19:57

1 answer

1

Try this:

/**
 * Em caso de erro retorna um IOException
 * Esta exeçnao deve ser tratada onde é chamado
 * @return
 * @throws IOException
 */
public String Listar ()  throws IOException{
       final  AssetManager assetManager = getResources().getAssets();
        inputStream = assetManager.open("arquivo.txt");//conteudo do que no diretorio assets
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String recebe_string = "";

        final StringBuilder stringBuilder = new StringBuilder();

        while ((recebe_string = bufferedReader.readLine()) != null) {

            stringBuilder.append(recebe_string); // copiamos a linha
            stringBuilder.append("\n"); // adicionando uma quebra

        }

        return stringBuilder.toString();

}

In your call:

final File file = new File(getFilesDir().getPath() + "/bd.txt"); // crio arquivo bd
if (!file.exists()) {
    try {
        fileWriter = new FileWriter(file, true);


        fileWriter.append(Listar());
        fileWriter.flush();



    } catch (IOException e) {

        Toast.makeText(this, "Não foi possível copiar o arquivo", Toast.LENGTH_SHORT).show();

    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (Exception e) {
            }
        }
    }
} else { // JA EXISTE
    System.out.println("arquivo ja existe");
}
    
19.10.2015 / 20:23