Text I/O Flashcards

1
Q

What classes are commonly used in Java for text file I/O?

A

Scanner for reading, PrintWriter for writing .
they allow reading/writing and numeric values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are the main topics covered in Java file processing?

A
  1. Input/Out (I/O)
  2. Reading from a File
  3. File name path
  4. Input token
  5. Valid input testing
    6.Line-based scanner
  6. Scanner on strings
  7. Writing output
    9.Dynamic file name input
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you read varies data types from the console?

A

nextInt() for integers
nextDouble() for decimals
nextLine() for full lines

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between an absolute path and a relative path?

A

Absolute paths specify the complete file location, while relative paths are relative to the program’s current directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name four numeric methods of the Scanner class.

A

nextByte()
nextShort
nextInt()
nextDouble()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What the difference between a physical and logical file?

A

Physical file = stored on disk
Logical file =File object used in code to access it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the syntax for reading from a file using Scanner?

A

Scanner input = new Scanner(new File(‘‘mydata,txt));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you count the number of words in a file?

A

Use a loop:
while (input . hasNext()) {
input .next (); count++;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is the difference between an absolute and relative file path?

A

Absolute: includes full directory structure
Relative: path from current working directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a token in Java input processing?

A

A unit of user input, separated by whitespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does Scanner treat file input?

A

As a stream of characters-token separated by whitespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does “consuming input” mean in scanner?

A

Reading a token and advancing the cursor past it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you compute change between values in a file?

A

Read current and text token, subtract to find change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what does the temperature program output?

A

Change in temperatures between pairs of days

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens if the file has fewer tokens than the loo[ expects?

A

Throws NoSuchElementException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what are two commons Scanner exceptions?

A

NoSuchElementException -end of input
InputMismatchException - wrong token type

17
Q

what does hasNextDouble() check?

A

Whether the next token can be read as a double.

18
Q

How can safely read an int from the keyboard?

A

Check with hasNextInt() before calling nextInt()

19
Q

How do you avoid reading past the end of a file?

A

Use hasNext() or hasNext<Types>() before reading</Types>

20
Q

How can you process an unknown number of values?

A

Use while (input.hasNextDouble()).

21
Q

How does the uploaded code handle varying file lengths?

A

Use a while loop instead of fixed for.

22
Q

How do you skip non-numeric data in a Scanner?

A

Check with hasNextDouble(), else call input.next() to skip

23
Q

What does else input.next(); do in a scanner?

A

Skips over any non-numeric token safely

24
Q

What is line-based scanning?

A

Reading one full line and processing its tokes separately.

25
How do you read a tokenize line?
String line = input.nextLine(); Scanner lineScan = new Scanner(line);
26
what happens when scanner reads a line with nextLine()?
it reads characters until \n, including tabs and spaces
27
How do you tokenize a string using Scanner?
Scanner scan = new Scanner("15 3.2 hello"); scan.nextInt(); // 15
28
How do you count words on each line in a file?
Nest two scanners: one file, one for each line.
29
How do you write output to a file in Java?
Use PrintStream or PrintWriter PrintStream out = new PrintStream (new File ("output.txt")); out.println(Hello file!);
30
what happens if the output file already exists?
It is overwritten. Be careful!
31
What is System.out in Java?
A printeStream object for console output.
32
How do you read a filename from the user?
Scanner scan = new Scanner(System.in); String file = scan.nextLine();
33
Name three useful methods in the File class.
exists() delete() length()
34
What cab PrintWriter write?
String, char, char[], int, long, float, double, boolean
35
How can you read web content using Scanner?
URL url = new URL("http://..."); Scanner input = new Scanner(url.openStream());