File I/O and Mini Project

Stream Object

A stream is an object that allows for the flow of data between your program and some I/O device or some file. If the flow is into your program, the stream is called an input stream. If the flow is out of your program, the stream is called an output stream.

PrinterWriter, Scanner, I/O Exception

Screen Shot 2017-04-26 at 4.41.31 PM.png

PrinterWriter Class is a stream class used to write to a text file. Scanner

Scanner class is used to read from the keyboard or to read from a text file. We are familiar with reading from the keyboard. To read from a text file, we need to replace the argument System.in (in the Scanner constructor) with a suitable stream that is connected to the text file. The syntax is:

 Scanner Stream_Object = new Scanner(new FileInputStream(File_Name));

An example can be:

Scanner inputStream = new Scanner(new FileInputStream("filename.txt"));

FileNotFoundException is a kind of IOException. If the program wants to open a file for reading but there is no such file, then a File NotFoundException will catch this error, print out the error message and continue the program.

Screen Shot 2017-04-26 at 5.02.49 PM.png

Write to a Text File

Screen Shot 2017-04-26 at 4.54.03 PM.png

We declare an outputStream from PrintWriter Class outside the try block, otherwise, it will be a local variable.

Add New File Method

Screen Shot 2017-04-26 at 4.51.05 PM

We use Scanner to read a String input from the keyboard and store it in filename. PrintWriter class only accept stream as argument so we use FileOutputStream Class to create a stream with the name of filename.txt. Then a blank file is created from FileOutputStream class and an object connected to the file is created from Printwriter Class.

Appending to a Text File Method

Screen Shot 2017-04-26 at 4.51.14 PM.png

If there already is a file named Filename, when you add a file called filename again, the old contents of the file are lost. If you want to append text to that file named filename, you should use the true.

Reading from a Text File

Screen Shot 2017-04-26 at 5.03.23 PM.png

It has the same idea with writing to a text file. Since we want to read the content in the file, we use inputStream.hasNextLine(), as long as there is next line of content, the system will output it.

Mini Project – The Most Popular Names

Screen Shot 2017-05-12 at 2.47.35 PM.png

Screen Shot 2017-05-12 at 6.10.57 PM.pngScreen Shot 2017-05-12 at 6.11.02 PM.png

Screen Shot 2017-05-12 at 6.11.14 PM.png

Screen Shot 2017-05-12 at 3.05.20 PMScreen Shot 2017-05-12 at 3.05.47 PM

Personal Reflection: I learned how to input and output files and how to read from and write to files which may be useful to my Internal Assesment since I want to create a database and store data from the program. I learned that Scanner Class can not only read from the keyboard but also files. There are more fascinating functionalities OOP has waiting for me to explore!


Work Cited

Savitch, Walter J, and Kenrick Mock. Absolute Java. 1st ed. Boston [etc.]: Perason, 2016. Print.

留下评论