110 likes | 277 Views
Day 48. 9.1 An Overview of Streams and File I/O. Upcoming Test. Next Tuesday – test on everything learned related to Inheritance Focus will be on multiple choice and less on coding. The Concept of a Stream. You are already using files to store your Java classes and programs
E N D
Day 48 9.1 An Overview of Streams and File I/O
Upcoming Test • Next Tuesday – test on everything learned related to Inheritance • Focus will be on multiple choice and less on coding
The Concept of a Stream • You are already using files to store your Java classes and programs • You can also use files to store input for a program or to hold output from a program • In Java, file I/O, as well as simple keyboard and screen I/O, is handled by streams
A stream is an object that either delivers data to their destination, such as a file or the screen, or takes data from a source, such as a file or the keyboard, and delivers data to your programs
The object System.out is the only output stream we have used so far • Objects of the class Scanner , which we have been using for keyboard input, are streams
A stream is a flow of data (characters, numbers or bytes consisting of binary digits) • If the data flow into your program, the stream is called an input stream • If the data flow out of your program … (for heaven’s sake, fill in the blanks)
Differences between Text Files and Binary Files • Files that you write and read using an editor are called text files (aka ASCII files as they contain data encoded using a scheme known as ASCII coding) • Binary files represent data in a way that is inconvenient to read with a text editor but can be written and ready by a program very efficiently
9.2 Text-File I/O • PrintWriteris the preferred stream class for writing to a text file • The first line of your code should be importjava.io.*; • This tells Java you will be using the java.io library
The file is created by writing outputStream = newPrintWriter(newFileOutputStream(“fileName.txt”);
Exercise WARNING: this may be hard Create a word search puzzle with the following conditions • The puzzle is 10x10 • Each character is created by the math class’s randomization feature (search it up) • Each character in the puzzle is also stored in a 2-D array • After the puzzle is generated, insert words relevant to this unit (along the vertical, horizontal and diagonal and make some intersect) • Print out the word search to a txt file