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

    VeryBIgCorp

    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 (Scanner.next()). 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:
    PHP:
    public String readFile(String paththrows FileNotFoundException {
              
    String ret "";
              
    Scanner s = new Scanner(new File(path));
              while(
    s.hasNext()){
                  
    ret += s.next()+" ";
              }
              return 
    ret;
    }
    If anybody has a more efficient example, please reply. I know mine probably isn't the quickest :p
     
  3. Offline

    V10lator

    Code:java
    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

    Njol

    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

    V10lator

    "This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)." (source: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashSet.html ) - 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

    Njol

    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 http://en.wikipedia.org/wiki/Java_collection_framework)
     
  7. Offline

    V10lator

  8. Offline

    Scyntrus

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

Share This Page