0

Problem

I'm trying to read the configuration file for Gearbox Software's 2012 Video game "Borderlands 2".

  • The problem i'm facing is that the input file i'm reading from is of 2,245 lines and 77,344 length whereas the number of values in the map in which i'm reading this file into is only 859 lines and 31,786 length.

  • It is not that the stream is getting closed prematurely because manual checking revealed that the last lines are there, it's the random lines in the middle that are missing.

  • i even tried Files.readAllLines() but i cannot seem to read more than 859 lines from the input file.

  • When i output this map to a new file using FileWriter, the number of lines is 860, with one extra blank line added in the middle of the program

  • I have checked the other threads for reading .ini files. I will use ini4j or regular expressions once i figure out what i'm doing wrong here.

My Map object

Map <String,String>config_vals=new LinkedHashMap<>();
   

My Objects for Input

InputStream IS= new FileInputStream("C:\\Users\\V\\BC\\WillowEngine.ini");
Reader R=new InputStreamReader(IS);
BufferedReader BFR=new BufferedReader(R);

Loop for reading the contents of the file

while((temp=BFR.readLine())!=null)
 {
    if(temp.indexOf("=")==-1)             //if not a key-value pair, put whitespace as value
     config_vals.put(temp,"");  
    else
    {
     int separator=temp.indexOf("=");     //separating a k-v pair from a file into map entry     
     config_vals.put(temp.substring(0,separator),temp.substring(separator+1,temp.length()));
    }
}
BFR.close();
System.out.println(config_vals.size());  // prints 859

I worked an hour or so yesterday and i'm unable to figure out what i'm doing wrong here.

Gestalt
  • 93
  • 10
  • 1
    Have you checked for duplicate keys? Maps only allow one value per key and you'd have to do something along `Map>` to represent several values per key. – Erk Feb 24 '21 at 06:01
  • 1
    And the empty line halfway through might represent empty lines in the original input file... – Erk Feb 24 '21 at 06:04
  • @Erk You're absolutely right, i forgot that maps don't process duplicate keys and the file has some duplicate keys – Gestalt Feb 24 '21 at 06:24
  • @Erk The file has empty lines before each header but my output file only has a single blank line at a particular point nowhere near a header. – Gestalt Feb 24 '21 at 06:26
  • 1
    yes, because the Map only allows one empty string key. The LinkedHashMap works like the HashMap but it will keep the keys in the order of the first inserted key. It will not however break the contract of a Map: "A map cannot contain duplicate keys; each key can map to at most one value." https://docs.oracle.com/javase/8/docs/api/java/util/Map.html – Erk Feb 24 '21 at 07:08

0 Answers0