You don't share what your delimiter is, but something like this will work:
final String DELIMITER = " "; // using space
String example = "one.xml two.xml three.xml";
List<String> items = Arrays.asList(example.split(DELIMITER));
for (String item : items) { // test output
System.out.println(item);
}
You'd probably be best just adding it to the List
when you read the file unless you need the String
representing the file contents for some other purpose. For example using Scanner
:
Scanner sc = new Scanner(new File("file.txt")); // default delimiter is whitespace
/**
* or if using a custom delimiter:
* final String DELIMITER = " "; // using space
* Scanner sc = new Scanner("file.txt").useDelimiter(DELIMITER);
*/
List<String> items = new ArrayList<>();
while (sc.hasNext()) {
items.add(sc.next());
}
for (String item : items) { // test output
System.out.println(item);
}
file.txt
one.xml
two.xml
three.xml