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
What have you done? What do you want to do (enter more details from
parser
)?– Christian Felipe
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
– R.Santos
I answered there fi!
– Christian Felipe