Copy file content in Assets directory to existing Android file

Asked

Viewed 705 times

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 doesn’t exist (first run), I wanted to copy what’s in txt that’s in the Assets directory to that 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’s on the 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?
}

}

1 answer

1


Try it like 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();

}

On 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");
}
  • I appreciate the reply the system creates the file only empty. returns error in final Assetmanager assetManager = getResources(). getAssets(); and fileWriter.append(p.List(); put b.list as I think the data class was missing

  • but it worked? Any doubt just post there ! Hugs

  • worked now. I did everything in one class only thanks

Browser other questions tagged

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