Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 programming: Reading a .ini-file and trying to get rid of newline-characters

Tags:

java

newline

ini

I'm using Netbeans IDE. For a school project I need to read an .ini-file, and get some specific information.

The reason I'm not using ini4j:

  • I have a section that has key values which are the same
  • I have sections that have no key-value inputs that I have to read information from

Example ini-file:

[Section]

Object1 5 m

number = 12

Object2 6 m
;Comment followed by white line

number = 1\

4

\ means the next command or white lines need to be ignored So the last part of the ini file actually means: number = 14

My task: I need to store the oject names with the corresponding length (meters) and number into a single string like this: Object1 has length 1m and number 12

My problem: I use a scanner with delimiter //Z to store the whole file into a single String. This works (if I print out the String it gives the example above).

I've tried this code:

String file = file.replaceAll("(\\.)(\\\\)(\\n*)(\\.)","");

If I try to only remove the newlines:

String file = file.replace("\n","");
System.out.println(file);

I get an empty output.

Thanks in advance !

like image 534
Agnaroc Avatar asked May 02 '15 11:05

Agnaroc


1 Answers

You are on right way. But logic is on wrong place. You actually need \n for your logic to recognize new value in your ini file.

I would suggest that you do not read entire file to the string. Why? You will still work with line from file one by one. Now you read whole file to string then split to single strings to analyze. Why not just read file with scanner line by line and analyze these lines as they come?

And when you work with individual line then simply skip empty ones. And it solves your issue.

like image 115
Izold Tytykalo Avatar answered Nov 14 '22 23:11

Izold Tytykalo