Reputation: 15
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("day.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("day.txt"));
System.out.println(reader.readLine());
}
The day.txt I have wrote some words before execute. If I change System.out.println with Writer, it will not be null. why?
Upvotes: 1
Views: 72
Reputation: 304
Create the writer instance after printing out to console. When the writer is initialized, the file is in use so you can't read it.
Upvotes: 0
Reputation: 44824
You are overwriting the same file when you do new FileWriter("day.txt");
change your code to
BufferedReader reader = new BufferedReader(new FileReader("day.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("day-new.txt"));
System.out.println(reader.readLine());
Upvotes: 4