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?
}
}
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
– Gustavo Rotondo
but it worked? Any doubt just post there ! Hugs
– Thiago Luiz Domacoski
worked now. I did everything in one class only thanks
– Gustavo Rotondo