java - Buffer string to array list, and split -
i buffering text file of 'arraylist lines' need split each line new arraylist parts, can find information each line , add data model have built, reason using arraylists because of there expandable properties, meaning wont need worry size of either line or text file.
the code below:
try(bufferedreader buffer = new bufferedreader(new filereader("src/sample.txt"))) { string currentline; arraylist<string> lines = new arraylist<string>(); arraylist<string> parts = new arraylist<string>(); //listiterator<string> lineitr = lines.listiterator(); while((currentline = buffer.readline()) != null) { lines.add(currentline); for(string line : lines) { parts.addall(line.split("\\s+")); } //lineitr.next(); //lineitr.set(currentline); //system.out.println(lineitr.next()); } } catch(ioexception e) { e.printstacktrace(); } } i having troubles parts.addall(line.split("\s+"); not understand why statement not iterate through lines, splitting , adding each part of string parts array list, misunderstanding here?
thanks babble
string.split() returns array of string . have use arrays.aslist() convert list .
parts.addall(line.split("\\s+")); above line should be:
parts.addall(arrays.aslist(line.split("\\s+"))); or :
collections.addall(parts, line.split("\\s+"));
Comments
Post a Comment