Text I/O Flashcards
What classes are commonly used in Java for text file I/O?
Scanner for reading, PrintWriter for writing .
they allow reading/writing and numeric values
what are the main topics covered in Java file processing?
- Input/Out (I/O)
- Reading from a File
- File name path
- Input token
- Valid input testing
6.Line-based scanner - Scanner on strings
- Writing output
9.Dynamic file name input
How do you read varies data types from the console?
nextInt() for integers
nextDouble() for decimals
nextLine() for full lines
What is the difference between an absolute path and a relative path?
Absolute paths specify the complete file location, while relative paths are relative to the program’s current directory.
Name four numeric methods of the Scanner class.
nextByte()
nextShort
nextInt()
nextDouble()
What the difference between a physical and logical file?
Physical file = stored on disk
Logical file =File object used in code to access it
what is the syntax for reading from a file using Scanner?
Scanner input = new Scanner(new File(‘‘mydata,txt));
How can you count the number of words in a file?
Use a loop:
while (input . hasNext()) {
input .next (); count++;
}
what is the difference between an absolute and relative file path?
Absolute: includes full directory structure
Relative: path from current working directory
what is a token in Java input processing?
A unit of user input, separated by whitespace.
How does Scanner treat file input?
As a stream of characters-token separated by whitespace.
What does “consuming input” mean in scanner?
Reading a token and advancing the cursor past it.
How can you compute change between values in a file?
Read current and text token, subtract to find change.
what does the temperature program output?
Change in temperatures between pairs of days
What happens if the file has fewer tokens than the loo[ expects?
Throws NoSuchElementException
what are two commons Scanner exceptions?
NoSuchElementException -end of input
InputMismatchException - wrong token type
what does hasNextDouble() check?
Whether the next token can be read as a double.
How can safely read an int from the keyboard?
Check with hasNextInt() before calling nextInt()
How do you avoid reading past the end of a file?
Use hasNext() or hasNext<Types>() before reading</Types>
How can you process an unknown number of values?
Use while (input.hasNextDouble()).
How does the uploaded code handle varying file lengths?
Use a while loop instead of fixed for.
How do you skip non-numeric data in a Scanner?
Check with hasNextDouble(), else call input.next() to skip
What does else input.next(); do in a scanner?
Skips over any non-numeric token safely
What is line-based scanning?
Reading one full line and processing its tokes separately.