Good afternoon I need to get the value of a text file and write each line in a txtedit, the file is generated by the code below:
byte[] dados;
File arquivo = new File(Environment.getExternalStorageDirectory(), "/AperamApps/DBQ/DBQmestre/dados.txt");
try {
if (!arquivo.exists()) {
//cria um arquivo (vazio)
arquivo.createNewFile();
}
//caso seja um diretório, é possível listar seus arquivos e diretórios
File[] arquivos = arquivo.listFiles();
//escreve no arquivo
FileWriter fw = new FileWriter(arquivo, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("#1 "+as1);
bw.newLine();
bw.write("#2 "+as2);
bw.newLine();
bw.write("#3 "+as3);
bw.newLine();
bw.write("#4 "+as4);
bw.newLine();
bw.write("#5 "+as5);
bw.newLine();
bw.write("#6 "+as6);
bw.newLine();
bw.write("#7 "+as7);
bw.newLine();
bw.write("#8 "+as8);
bw.newLine();
bw.write("#9 "+as9);
bw.newLine();
bw.write("#10 "+as10);
bw.newLine();
bw.close();
fw.close();
//faz a leitura do arquivo
FileReader fr = new FileReader(arquivo);
BufferedReader br = new BufferedReader(fr);
//equanto houver mais linhas
while (br.ready()) {
//lê a proxima linha
String linha = br.readLine();
//faz algo com a linha
System.out.println(linha);
}
br.close();
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
And here's the code I'm trying to use to read the file:
BufferedReader br = null;
try {
br = new BufferedReader (new FileReader ("/sdcard/AperamApps/DBQ/DBQmestre/dados.txt"));
for (String linha = br.readLine(); linha != null; linha = br.readLine()){
while ((linha = br.readLine()) != null) {
System.out.println(linha);
br.readLine();
txtcoluna1.setText(linha);
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
The code can even read, but it always displays the last line of the text file.