Parse a txt file

Asked

Viewed 988 times

0

Good morning,

I created a *.txt file with certain paths, now I need to put these paths within variables to call them in the application. I thought I’d use a parser to get this information, but I could not get any example. If you can help me thank.

inPath = C: Input --- Input directory
outPath = C: Output -- Output directory
inProcess = C: In Processing -- Directory Processing

  • What have you done? What do you want to do (enter more details from parser)?

  • I tried to use the split method, but I didn’t get what I wanted. Then I saw examples of parsers for XML files, but for TXT files I couldn’t find any examples. I also don’t know if it is possible to apply parser method in a TXT file

  • I answered there fi!

2 answers

4


Because it is a configuration file I would recommend you to use a file of type .ini, but unfortunately Java does not have a standard library to do this kind of operation, if you agree to do so you can download the library from the address: http://ini4j.sourceforge.net/ There is also the option to download it directly from Maven.

Download and import the library, you need to put a section in your file so that it is correctly interpreted with a file of type .ini. Example:

[cfg]
inPath=C: Input ; Input directory
outPath=C: Output ; Output directory
inProcess=C: In Processing ; Processing directory

And the code would look like this:

import java.io.File;
import java.io.IOException;

import org.ini4j.Ini;
import org.ini4j.IniPreferences;
import org.ini4j.InvalidFileFormatException;

public class Teste {
    public static void main(String[] args) throws InvalidFileFormatException, IOException {
        Ini ini = new Ini(new File("meucfg.ini"));
        java.util.prefs.Preferences prefs = new IniPreferences(ini);        
        String inPath = prefs.node("cfg").get("inPath", "null").split(";")[0].trim();
        String outPath = prefs.node("cfg").get("outPath", "null").split(";")[0].trim();
        String inProcess = prefs.node("cfg").get("inProcess", "null").split(";")[0].trim();

        System.out.printf("inPath: %s\noutPath: %s\ninProcess: %s\n", inPath, outPath, inProcess);
    }    
}

Exit:

inPath: C: Entry
outPath: C: Exit
inProcess: C: In Processing

  • i could not create a file. ini

  • file . ini I can create in Notepad?

  • that only changes the extension from txt to ini

  • perfect, gave right now

0

Well, I don’t quite know what your processing on parser. Considering that you take line by line and process, I made this solution, that when processing the line already saved in the output directory, without using the directory inProcess, because I don’t know if you really need.

String inPath = "C:\Entrada\dados.txt"
String outPath = "C:\Saida\dados.txt"
FileWriter file = new FileWriter(outPath);
Scanner in = new Scanner(new FileReader(inPath));
while (in.hasNextLine()) {
     String line = in.nextLine();
     // faz processamento com vários métodos para auxiliar
     // line.split(regex);
     // line.compareTo(anotherString);
     // line.contains(string);
     // line.matches(regex);
     // ... vários outros
     System.out.println(line);
     file.write(line_processed);   // salva linha já processada
}
file.close();

Obviously that’s not the only solution!

Browser other questions tagged

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