You could perhaps use regular expression in this case:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class testeajuda {
public static void main(String[] args) throws FileNotFoundException, IOException {
File diretorio = new File("C:\\testes\\"); //Diretório de entrada
File[] arquivos = diretorio.listFiles();
if (arquivos != null) {
for (int i = 0; i < arquivos.length; ++i) {
if (arquivos[i].getName().endsWith("txt")) {
File f = arquivos[i];
FileReader reader = new FileReader(f);
BufferedReader input = new BufferedReader(reader);
String linha = "";
String teste = "";
while ((linha = input.readLine()) != null) {
Matcher matcher = Pattern.compile(":\\s\\w*").matcher(linha); // Primeiro Filtro (Pega tudo que tem :, espaço, e tudo que vem depois que seja alfanumerico)
while (matcher.find()) {
Pattern pattern = Pattern.compile(":\\s"); // Segundo Filtro (Elimina o ":" e o espaço)
Scanner scanner = new Scanner(matcher.group()).useDelimiter(pattern);
Matcher matcher2 = pattern.matcher(teste);
while (scanner.hasNext()) {
teste = scanner.next();
System.out.println(teste); // retorna apenas "Ricardo"
}
}
}
}
}
}
}
}
You didn’t get it with the for?
– Roknauta
Related: How to read data from txt files using Java?
– user28595
@Douglas unfortunately not
– Tacio souza
Does the default have to be the same? If you are not looking to use XML or JSON it already makes it easy. And in this case I believe that only with is really, is what basically the file patterns "tagged" use.
– Victor Magalhães
Even in JSON there are tools that already do Java/String JSON conversion and vice versa. Example
– Gustavo Cinque
Thank you all I am beginner and did not know yet JSON
– Tacio souza
You could do one for and within it an if, which prevents it from returning any line text. A hint, use the lenght with an array to highlight what you don’t want, in the case "name:".
– user47018