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?
}
}