You can use Scanner
class, which provides a Scanner#nextInt()
method to read the next token as int. Now, since your integers are comma(,
) separated, you need to set comma(,
) as delimiter in your Scanner
, which uses whitespace
character as default delimiter. You need to use Scanner#useDelimiter(String)
method for that.
You can use the following code:
Scanner scanner = new Scanner(new File("demo.txt"));
scanner.useDelimiter(",");
while (scanner.hasNextInt()) {
System.out.println(scanner.nextInt());
}
2
solved How to read portions of text from a .txt file in Java?