Create a new class that writes to the included txt file. Use a BufferedWriter to write the following text to the file: What tookyou so long? Well, you know, Master, I couldn'tfind a speeder that I really liked... There he is. ...with an open cockpit and the right speed capabilities. I thought I already did. Only in your mind, my veryyoung apprentice. Make sure that all the line breaks are correct. Hint: You can access the file via its relative path by just stating its name: conversation.txt. No need to have the complete path to the file.
时间: 2023-12-03 21:43:56 浏览: 32
Here's an example of a class that writes to the included txt file using a BufferedWriter:
```
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFile {
public static void main(String[] args) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("conversation.txt"));
writer.write("What took you so long?\n");
writer.write("Well, you know, Master, I couldn't find a speeder that I really liked...\n");
writer.write("There he is.\n");
writer.write("...with an open cockpit and the right speed capabilities.\n");
writer.write("I thought I already did.\n");
writer.write("Only in your mind, my very young apprentice.\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
This class creates a BufferedWriter that writes the text to the file "conversation.txt" using the `write()` method. Each line is terminated with a newline character `\n` to ensure correct line breaks. Finally, the `close()` method is called on the BufferedWriter to release any resources it is using.