the code I have written:
import java.io.*;
class Main {
public static void main(String[] args) {
Writer text = new FileWriter("test.txt");
BufferedWriter wrt = BufferedWriter(text);
for (int i = 0; i < 10; i++){
wrt.write(i);
wrt.newLine();
}
wrt.flush();
wrt.close();
}
}
I’m writing a simple method to create a file and write numbers on each line. I’m utilizing a filewriter to create the actual file, and a buffered writer to actually put in the numbers. When I run my code, I recieve this error:
./Main.java:5: error: cannot find symbol
BufferedWriter wrt = BufferedWriter(text);
^
symbol: method BufferedWriter(Writer)
location: class Main
1 error
According to the api, Writer can be a parameter for the BufferedWriter, so that shouldn’t be the issue. I spelled everything right as well. API BufferedWriter parameters
>Solution :
You need to use new keyword to create instance of BufferedWriter. please see below working code.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Writer text = new FileWriter("test.txt");
BufferedWriter wrt = new BufferedWriter(text);
for (int i = 0; i < 10; i++) {
wrt.write(i);
wrt.newLine();
}
wrt.flush();
wrt.close();
}
}