In Apache Commons, I can write:
LineIterator it = IOUtils.lineIterator(System.in, "utf-8");
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
Is there anything similar in Guava?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, to start with…this isn’t something you particularly need a library for, given that it’s doable with just the straight JDK as
but if you want it to be more collections-y Guava provides
List<String> CharStreams.readLines(Readable).I think we don’t provide an
Iteratorbecause there isn’t really any good way to deal with the presence ofIOExceptions. Apache’sLineIteratorappears to silently catch anIOExceptionand close the iterator out, but…that seems like a confusing, risky, and not always correct approach. Basically, I think the “Guava approach” here is either to either read the whole input into aList<String>all at once, or to do theBufferedReader-style loop yourself and decide how you want to deal with the potential presence ofIOExceptions.Most of Guava’s I/O utilities, in general, are focused on streams that can be closed and reopened, like files and resources, but not really like
System.in.