How to Get Data from a Java Text File?

Asked

Viewed 430 times

0

I have a text file with some data in the following notation [[name: Ricardo]] and I need a function that identifies this tag in the middle of the file, and returns the string with the name in question. I have been tempted to do it with a loop running through the archive in search of the brackets, but I wonder if there is any way to accomplish this that is simpler and faster.

  • You didn’t get it with the for?

  • @Douglas unfortunately not

  • 2

    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.

  • Even in JSON there are tools that already do Java/String JSON conversion and vice versa. Example

  • Thank you all I am beginner and did not know yet JSON

  • 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:".

Show 2 more comments

1 answer

2

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

                }
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.