Parsing Configuration as an array list.

Discussion in 'Plugin Development' started by Daniel Heppner, Apr 29, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Daniel Heppner

    Okay, so it's been a really long time since I've posted on here, and I'm sure you've all missed me. Anyway, I'm just wondering how I can parse a text file (not yml) as an array list.
    So say I put I bunch of words in the file, line by line, and each line will be another item in the array list. Not sure how to do this, I haven't really messed with I/O much yet.
  2. Offline


    Read the file into a String, split the contents using the newline delimiter, and then loop through that array and add each String to the ArrayList.

    To read the file, instantiate a new File object with the path to your text file, and then instantiate a new Scanner object with its parameter being the File object. Now, run a while loop that checks if the scanner has anything left in its iteration (Scanner.hasNext()). In the loop, append your string with the next String in the file ( Obviously, replace Scanner with your scanner object you made earlier.

    Now that you've read the contents into the String, just run the split("\n") method to break it into a String array. This can now be iterated upon to add to the ArrayList.

    If you need an example, here's a small snippet I've written:
    public String readFile(String paththrows FileNotFoundException {
    String ret "";
    Scanner s = new Scanner(new File(path));
    ret +=" ";
    If anybody has a more efficient example, please reply. I know mine probably isn't the quickest :p
  3. Offline


    1. BufferedReader reader = new BufferedReader(new FileReader(theFile));
    2. LinkedHashSet<String> set = new LinkedHashSet<String>(); //Use a normal HashSet or ArrayList if the order of the lines isn't important
    3. while(reader.ready())
    4. set.add(reader.readLine());
    5. String[] theArray = set.toArray(new String[0]); //No need for this if you don't really need a array

    but this will delete doubled lines...

    //EDIT: VeryBIgCorp I would do a (simple) DOS2UNIX conversion before the split:
    theString.replaceAll("\r", "").split("\n");
  4. Offline


    Use a List, e.g. a LinkedList. Sets do not guarantee that the order of the elements is consistent, but lists do since it's their purpose.
    IIRC Some OSes only uses \r, not \n, thus you should spilt at "\r\n?|\n".
  5. Offline


    "This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)." (source: ) - It's not the difference between list or set, it's between linked or not. ;)
    That's why I sayed "simple". But what OS puts a \r somewhere in the line? I even had some issues (java thought it was the end of the string) when I had a \r somewhere in a string :p

    //EDIT: Whoops, missreaded. What OSes use just \r? :confused:
  6. Offline


    Then LinkedHashSet is special, because in general Sets can rearrange elements as they desire, only Lists are required to maintain the order of elements (see
  7. Offline


  8. Offline


    If you're writing and reading the textfile from a Java application, and not by hand, you can try "Serializing" the list object.
Thread Status:
Not open for further replies.

Share This Page