simple java input/output - penn engineeringrivanov/cit590_fall17/... · • there is a limit on the...
TRANSCRIPT
![Page 1: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/1.jpg)
Simple Java Input/Output
![Page 2: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/2.jpg)
Prologue
“They say you can hold seven plus or minus two pieces of information in your mind. I can’t remember how to open files in Java. I’ve written chapters on it. I’ve done it a bunch of times, but it’s too many steps. And when I actually analyze it, I realize these are just silly design decisions that they made. Even if they insisted on using the Decorator pattern in java.io, they should have had a convenience constructor for opening files simply. Because we open files all the time, but nobody can remember how. It is too much information to hold in your mind.”
—Bruce Eckel, http://www.artima.com/intv/aboutme2.html
![Page 3: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/3.jpg)
Streams
• All modern I/O is stream-based
• A stream is a connection to a source of data or to a destination for data (sometimes both)
• An input stream may be associated with the keyboard
• An input stream or an output stream may be associated with a file
• Different streams have different characteristics:– A file has a definite length, and therefore an end
– Keyboard input has no specific end
![Page 4: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/4.jpg)
How to do I/O
import java.io.*;
• Open the stream
• Use the stream (read, write, or both)
• Close the stream
![Page 5: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/5.jpg)
Why Java I/O is hard
• Java I/O is very powerful, with an overwhelming number of options
• Any given kind of I/O is not particularly difficult
• The trick is to find your way through the maze of possibilities
![Page 6: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/6.jpg)
Opening a stream
• There is data external to your program that you want to get, or you want to put data somewhere outside your program
• When you open a stream, you are making a connection to that external place
• Once the connection is made, you forget about the external place and just use the stream
![Page 7: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/7.jpg)
Example of opening a stream
• A FileReader is a used to connect to a file that will be used for input:– FileReader fileReader = new FileReader(fileName);
• The fileName specifies where the (external) file is to be found
• You never use fileName again; instead, you use fileReader
![Page 8: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/8.jpg)
Using a stream
• Some streams can be used only for input, others only for output, still others for both
• Using a stream means doing input from it or output to it
• Exampleint charAsInt = fileReader.read( );
• The fileReader.read() method reads one character and returns it as an integer, or -1 if there are no more characters to read
• The meaning of the integer depends on the file encoding (ASCII, Unicode, other)
• You can cast from int to char:char ch = (char)fileReader.read( );
![Page 9: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/9.jpg)
Manipulating the input data
• Reading characters as integers isn’t usually what you want to do
• A BufferedReader will convert integers to characters; it can also read whole lines
• The constructor for BufferedReader takes a FileReaderparameter:
BufferedReader bufferedReader = new BufferedReader(fileReader);
• To read a line with a BufferedReader:String s = bufferedReader.readLine( );
• A BufferedReader will return null if there is nothing more to read
![Page 10: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/10.jpg)
Closing
• A stream is an expensive resource
• There is a limit on the number of streams that you can have open at one time
• You should not have more than one stream open on the same file
• You must close a stream before you can open it again
• Always close your streams!
• Java will normally close your streams for you when your program ends, but it isn’t good style to depend on this
![Page 11: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/11.jpg)
Text files
• Text (.txt) files are the simplest kind of files– Text files can be used by many different programs
• Formatted text files (such as .doc files) also contain binary formatting information– Only programs that “know the secret code” can make sense of
formatted text files
– To other programs, such files are just unintelligible binary information
• Compilers, in general, work only with text– Comma-separated value (.csv) files are also easy to work with
![Page 12: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/12.jpg)
Putting it all together
1. Create a FileReader– Need to catch a FileNotFoundException
2. Create a BufferedReader
3. Read your file line by line using the BufferedReader– Need to catch an IOException
4. Close the BufferedReader
• Eclipse example: BufferedReaderExample.java
![Page 13: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/13.jpg)
How did I figure that out?
• I wanted to read lines from a file
• I thought there might be a suitable readSomething method, so I went to the API Index
• I found a readLine method in several classes; the most promising was the BufferedReader class
• The constructor for BufferedReader takes a Reader as an argument
• Reader is an abstract class, but it has several implementations, including InputStreamReader
• FileReader is a subclass of InputStreamReader
• There is a constructor for FileReader that takes as its argument a (String) file name
![Page 14: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/14.jpg)
Output streams
• Files are usually written to using an OutputStream– Of course, it is once again an abstract class
• A useful subclass of OutputStream is FileOutputStream– System.out is actually a PrintStream, which is also a subclass of
OutputStream
• SyntaxFileOutputStream fos = new FileOutputStream(filename, append);
• If append is true, you will append to the end of the file
• Of course, a is FileOutputStream not really useful (but there are exceptions…) because it only writes a byte/int at a time
![Page 15: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/15.jpg)
Enter the PrintWriter
• The class that is actually useful when writing to a file is a PrintWriter
• A PrintWriter takes a FileOutputStream as input
• SyntaxPrintWriter pw = new PrintWriter(fos);
• With a PrintWriter, you can write strings or entire lines to file
• The most useful functions are print and println which work exactly like System.out.print and System.out.println
![Page 16: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/16.jpg)
Flushing the buffer
• When you put information into a buffered output stream, it goes into a buffer
• The buffer may or may not be written out right away
• If your program crashes, you may not know how far it got before it crashed
• Flushing the buffer forces the information to be written out– Buffers are automatically flushed when the program ends normally
– Usually it is your responsibility to flush buffers if the program does not end normally
• The PrintWriter can do the flushing for youpw.flush()
![Page 17: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/17.jpg)
Close your PrintWriter
• Like any other stream this needs to be closed, too
• Java will close it when your program ends but this is bad style
• If your program crashes, sometimes Java will get confused and won’t close it properly, so make sure you always close it!
![Page 18: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/18.jpg)
Putting it all together
1. Create a FileOutputStream– Need to catch a FileNotFoundException
2. Create a PrintWriter
3. Write strings or lines using the PrintWriter
4. Close the PrintWriter
• Eclipse example: PrintWriterExample.java
![Page 19: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/19.jpg)
The Scanner class
• Takes any InputStream as input to the constructor– Could be a file
Scanner scanner = new Scanner(new File(filename));
– Could be System.in (the keyboard)
Scanner scanner = new Scanner(System.in);
– Both throw FileNotFoundException
• You can even read from a StringScanner scanner = new Scanner(myString);
– This can useful for parsing a String
![Page 20: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/20.jpg)
Several useful Scanner methods
• Check if there is more stuff to read– Different methods depending on the type of input
– sc.nextBoolean(), sc.nextByte(), sc.nextShort(), sc.nextInt(), sc.nextLong(), sc.nextFloat(), sc.nextDouble()
• You can read the whole remaining line– We already saw the method nextLine()
– Also reads the whole string until the user presses Enter
• You can also read token by token– A token can be an int/String/whatever the Scanner was set up with
– Use the next() method for this
– Probably less useful than nextLine()
![Page 21: Simple Java Input/Output - Penn Engineeringrivanov/cit590_fall17/... · • There is a limit on the number of streams that you can have open at one time • You should not have more](https://reader036.vdocuments.mx/reader036/viewer/2022070904/5f72ac1539619900b560f7a0/html5/thumbnails/21.jpg)
Formatted output
• Java 5 has a printf method, similar to that of C
System.out.printf(someString + formatCode, arguments);
• Each format code is a String of the form: % width code
– The width is the number of characters that are output (with blank fill)
– By default, output is right-justified
– A negative width means to left-justify the output
• Some values for the code are s for strings, d for integers, f for floating point numbers, b for booleans
– For floating point numbers, the width has the form total.right, where total is the total width and right is the number of digits to the right of the decimal point
– There are a huge number of options for formatting dates, which we won’t cover
• Eclise example: PrintfClass.java