Create a new class that reads the conversation.txt line by line and prints it to the console. In this class you should implement two different methods to read the file: 1. Implement a method using a Scanner that reads the file. (Successfully printing file content to Console: 1P) 2. Implement a method that uses a BufferedReader to read the file. (Successfully printing file content to Console: 1P) Finally, alter the second method that reads the file line by line using a BufferedReader. After printing the first two lines, make your programme wait for the user to press enter before printing the next two lines. Continue this until all the lines are printed. Once the end of the file is reached, the programme should terminate so that when the user presses enter again, nothing bad happens. Hint: You can use a Scanner which reads from the system input stream like this Scanner s = new Scanner(System.in); to wait for user input.
时间: 2024-02-13 09:03:01 浏览: 21
Sure, here's an example code for the requested class:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class FileReaderClass {
public static void readWithScanner() {
try {
Scanner scanner = new Scanner(new FileReader("conversation.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
public static void readWithBufferedReader() {
try {
BufferedReader reader = new BufferedReader(new FileReader("conversation.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
public static void readWithBufferedReaderAndPause() {
try {
BufferedReader reader = new BufferedReader(new FileReader("conversation.txt"));
String line;
int count = 0;
while ((line = reader.readLine()) != null) {
System.out.println(line);
count++;
if (count == 2) {
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
count = 0;
}
}
reader.close();
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
```
This class includes three methods that read the specified file name `conversation.txt`.
The first method `readWithScanner()` uses a `Scanner` to read the file and prints each line to the console.
The second method `readWithBufferedReader()` uses a `BufferedReader` to read the file and prints each line to the console.
The third method `readWithBufferedReaderAndPause()` also uses a `BufferedReader` to read the file, but it pauses after every two lines and waits for the user to press the enter key before continuing. To do this, we use a counter variable `count` that increments every time a new line is read. When the counter reaches two, the program waits for the user input using a `Scanner` object that reads from the system input stream. After the user presses enter, the counter is reset to zero and the program continues to read the next two lines until the end of the file is reached.
You can call these methods from anywhere in your code by simply invoking `FileReaderClass.readWithScanner()`, `FileReaderClass.readWithBufferedReader()`, or `FileReaderClass.readWithBufferedReaderAndPause()`.