Fifth Week of Class and Makin’ Text Files | Tiffany R. White Blog

Fifth Week of Class and Makin’ Text Files

Reading time: 2 mins

Java

So this is my fifth week at the University of Pittsburgh, in their CGS program, self-directed study. CGS stands for College of General Studies. I knew I’d have an easier time getting in there and working my way to The Dietrich School of Arts & Sciences Computer Science program.

My self-directed study? Computer Science all the way. I do have some gen eds to take so I am taking Chinese Thought this semester and will probably have to take a language course. I will also have to take Calc 1 & 2 and Stats.

I am extremely happy at Pitt right now, much happier than I was anywhere else. That’s not a slight against community colleges– they gave me a leg up when I needed it. If it wasn’t for HACC (specifically, CCAC isn’t great when you compare the two to be honest) I probably wouldn’t have been able to make it to a full on university. I always knew I was university material. Always. I may not have been able to go when I was young because of circumstances well out of my control but deep down I knew I could go, when I got my stuff together.

So I am in my fifth week and I have homework like mad. Ha. I am excited to do it.

We are doing file input and output in Java. We are writing to a text file with FileWriter. You can also append text to a file doing PrintWriter. Here is an example I found at the University of Texas website:

import java.io
 public class WriteTextFile
 {
 public static void main (String [] args) throws IOException
 {
 File outFile = new File ("output.txt");
 FileWriter fWriter = new FileWriter (outFile);
 PrintWriter pWriter = new PrintWriter (fWriter);
 pWriter.println ("This is a line.");
 pWriter.println ("This is another line.");
 pWriter.close();
 }



import java.io
 public class WriteTextFile
 {
 public static void main (String [] args) throws IOException
 {
 File outFile = new File ("output.txt");
 FileWriter fWriter = new FileWriter (outFile);
 PrintWriter pWriter = new PrintWriter (fWriter);
 pWriter.println ("This is a line.");
 pWriter.println ("This is another line.");
 pWriter.close();
 }




}
Here in the header you are importing the I/O package for Java and creating and writing text to a new file called “output.txt”, using
FileWriter fWriter = new FileWriter (outFile)
to write to the file after declaring the FileWriter class and then outputting the file. You then declare the
PrintWriter
class to append the proceeding lines to the the text file.
pWriter.close()
closes the file in a similar way that Python closes text files after you open and append text to them. Update: Seems, after I went to class today, that you open a file with
PrintWriter
and append to a file using
FileWriter
I guess I got confused there reading that code from UT.

Share

No Ads. No Sponsored Content. Ever.

I've decided that I no longer want to have sponsored posts on this blog. I also, though as tempting as it may sound, don't want ads; even if I got big enough for Carbon ads I don't want them. I want to post free, engaging content for beginning/junior devs. This means that I won't have Setapp posts or affiliates on this blog, though I will keep them on the Uses page. If you enjoy the content, buy me a coffee. Just click the button below.


Coffee Buy me a coffeeBuy me a coffee


Webmentions 0

No webmentions were found.

Newsletter