My question is about line (edit: 19), where the new PrintWriter is created with the constructor taking the FileWriter fw as a parameter. I don’t understand the use of chaining the BufferedWriter bw to FileWriter if it isn’t used later on in the actual writing. Can Java apply chaining in a way that bw still somehow affects the rest of the program?
16. try {
17. FileWriter fw = new FileWriter(test);
18. BufferedWriter bw = new BufferedWriter(fw, 1024);
19. PrintWriter out = new PrintWriter(fw);
20. out.println("<html><body><h1>");
21. out.println(args[0]);
22. out.println("</h1></body></html>");
23. out.close();
24. bw.close();
25. fw.close();
26. }catch(IOException e) {
27. e.printStackTrace();
28. }
I think it is probably a typo and they meant to use bw as the parameter for PrintWriter out but like the title says, I’m new to this.
Thanks to all in advance.
You are correct, this looks like a typo.
Line 18 should have no affect on the outcome of running this code.
The BufferedWriter Javadoc shows a normal example of how FileWriter, BufferedWriter and PrintWriter are typically used.