How to read portions of text from a .txt file in Java? -
i wondering, there way read parts of string in .txt file? example, if "1,5,10,20" in .txt file, can tell java save "1" int, save "5" different int, , on? hope made clear enough!
thanks guys!
p.s know how read whole line of text in .txt file in java using bufferedreader, not how read parts of it.
you can use scanner
class, provides scanner#nextint()
method read next token int. now, since integers comma(,
) separated, need set comma(,
) delimiter in scanner
, uses whitespace
character default delimiter. need use scanner#usedelimiter(string)
method that.
you can use following code:
scanner scanner = new scanner(new file("demo.txt")); scanner.usedelimiter(","); while (scanner.hasnextint()) { system.out.println(scanner.nextint()); }
Comments
Post a Comment