-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessFile.java
More file actions
82 lines (67 loc) · 2.52 KB
/
Copy pathProcessFile.java
File metadata and controls
82 lines (67 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* ProcessFile.java
*
* Illustrates how to:
* print the lines of a file using iteration
* print the lines of a file using recursion
* print the lines of a file in reverse order using recursion
*
* Note: Printing in reverse order is easy using recursion.
* To do this using iteration would require an extra data structure
* to store the lines as they are read.
* We don't need an extra data structure when using recursion
* because the lines are stored in the stack frames for the method calls.
*/
import java.io.*;
import java.util.*;
public class ProcessFile {
// Uses iteration to print the lines of a file from first to last.
public static void print(Scanner input) {
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
}
}
// Uses recursion to print the lines of a file from first to last.
public static void printRecursive(Scanner input) {
// base case
if (!input.hasNextLine()) {
return;
}
// recursive case:
// (a) print the current line
String line = input.nextLine();
System.out.println(line);
// (b) make a recursive call to print the rest of the file
printRecursive(input);
}
// Uses recursion to print the lines of a file in reverse order,
// from first to last.
public static void printRecursiveReverse(Scanner input) {
// base case
if (!input.hasNextLine()) {
return;
}
// recursive case:
// (a) read the current line
String line = input.nextLine();
// (b) make a recursive call to print the rest of the file
printRecursive(input);
// (c) print the current line *after* the rest of the file
// has been printed
System.out.println(line);
}
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("name of file: ");
String filename = console.nextLine();
Scanner input = new Scanner(new File(filename));
print(input);
// reopen the file to start over at the beginning
input = new Scanner(new File(filename));
printRecursive(input);
// reopen the file to start over at the beginning
input = new Scanner(new File(filename));
printRecursiveReverse(input);
}
}