Check out a free preview of the full Java Fundamentals course

The "Print to File" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates updating the printer from System to PrintWriter and auto-closing both resources within the try block.

Preview
Close

Transcript from the "Print to File" Lesson

[00:00:00]
>> So, I want to change the printer from System to PrintWriter. PrintWriter is also an auto-closable class, so this will be a way that we can show, how we can declare to have them in there. So let's go back to our class. We'll call this file. Let's refactor it.

[00:00:31]
And I'm gonna call this input file. All right, and then I want an output file. So I wanna say file, output file = new file and this one will be under files and we'll call it output.txt Okay and then I want Inside of here, where we are printing to the console instead of printing to the console and like print to a new file.

[00:01:15]
So we are reading from one file and printing as contents out to another file. So I wanna make a fileReader. FileReader is also a auto-closeable, so I wanna put it right here inside of this try. So the way you would do this is inside of here, after the first one, you would add a semicolon.

[00:01:39]
That's one statement. Then you could add as many other statements that you want. So we'll say printWriter, which is a java.io class. And we'll call this one fileWriter equals new. And we're going to instantiate this with print writer. And then we can give it the output file that we want to print to.

[00:02:05]
You don't need you can put a semicolon at the end but on the last one you don't need a semicolon. And then here I'm gonna print there. So we'll say fileWriter.println and it'll print what I read from the reader. Okay.
>> Why does it not need a semicolon after line?

[00:02:37]
16 it looks like.
>> Yeah, just because it's the last one in the list, so it's kind of implied, but you could definitely add one if you wanted to. Did it give me a warning unnecessary semicolon. And wow. Pretty bossy.
>> I've just never like seen very many Java conventions are just

[00:03:00]
>> Yeah, no, I know. I'm not sure I'm not sure why they set this up like that. I guess because it's inside of a set of parentheses maybe. Like it is kind of weird to say like another one that's coming almost like in JSON where you have commas after like each thing.

[00:03:17]
And on the last one you don't put another comma it just feels a little weird it gives me that vibe. All right this is still pretty, it's a little complex, right? The thing to note about these auto closable resources is that it's going to close them in the reverse order.

[00:03:37]
So if that matters for you at all, then you need to put them in the way that you want them, put them in a reverse order that you want them closed, right? So, for example the writer is closing before the reader. If you needed the input file closed before the output file, if that mattered at all, you would just switch the order of these.

[00:03:59]
But it's going to close from bottom up.
>> First in, last out.
>> Say it again? First in, last out? First in, last out. Yes, that's correct. That's correct. Questions on tri-web resources? All right, very nice.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now