Create a new class that writes to the included txt file. Use a BufferedWriter to write the following text to the file
时间: 2024-02-05 07:03:07 浏览: 27
Sure, here's an example code snippet that creates a new class that writes to a text file using a BufferedWriter:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
String fileName = "example.txt";
String textToWrite = "This is some example text to write to the file.";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(textToWrite);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
In this example, we create a new BufferedWriter object and pass in a FileWriter object with the file name we want to write to. We then use the `write` method of the BufferedWriter to write the text to the file. The `try` block with resources ensures that the writer is properly closed after the write operation is complete, even if an exception is thrown.