0
good afternoon need to take 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.
Good afternoon, thanks for the reply, I fixed the code but I still have the same problem; Bufferedreader br = null; Try ' br = new Bufferedreader (new Filereader ("/sdcard/Aperamapps/DBQ/Dbqmaster/data.txt"); for (String line = br.readline(); line != null; line = br.readline()){ Stringbuilder Sb = new Stringbuilder(); Sb.append(line); txtcoluna1.setText(Sb.toString()); tch (Ioexception e) { e. printStackTrace(); }
– Matheus Arruda
The
txtcoluna1.setText(sb.toString());
has to be put out of the loops at the end.– ramaral
Ok, now txtcoluna1 displays the entire text file, starting from item 1 to item 10. How do I filter this data, for example: My application has 10 txtedit (txtcoluna1, txtcoluna2, txtcoluna3...txtconlunaN), and the text file has 10 lines, as I put line 1 to be displayed in txtcoluna1, line 2 in txtcoluna2 ?
– Matheus Arruda
Use a Arraylist<String> instead of Stringbuilder.
– ramaral