For this matter, a file is just a sequence of bytes. Each byte may represent many different things. Files with .txt extension are supposed to be interpreted as a long sequence of characters (what we usually call "a string"). That is, each byte (or a very few number of bytes) is interpreted as a character/entity, which may be a letter, a digit [0-9], a punctuation mark, etc. Some of these characters/entities may have special meanings, like "end of line (of text)".
When you're reading or writing a file that's really a text file, and you know that some (sub)strings represent numbers, you can do the appropriate conversion (like with any number "contained" in a string). When your code calls Scanner.nextInt()
, it reads into a string a sequence of bytes that represent digits [0-9], stoping when it finds any other character/entity, and then converts that string to a "real" integer. When your code calls System.out.println()
, it does exactly the opposite: converts an integer to a string (by means of an implicit .toString()
).
Please be aware that in text files, as in strings, the code (ie. the exact sequence of bits) used to represent a given character/entity is actually arbitrary and depends on a chosen "character set" (like ASCII, EBCDIC, ISO8859*, some of the Unicode variantes, etc).