Title: JAVA
1????? ????? ????? ??? JAVA
2References
- ???? "????? ?????? ?????", ??????? ????? ?????,
???????. - ???? "?????? ???? ???????", ??????? ?????? ??????
??????, ???????.
3I/O Streams in Java
Introduction - 1
- Definition
- Stream is a flow of Data.
- characters read from a file
- bytes written to the network
-
- The information can be read/written sequentially
from/to the stream. - The assumption made on the stream the
information passes in FIFO order.
4Introduction - 2
- No matter where the data is coming from or going
to and no - matter what its type, the algorithms for
sequentially reading - and writing data are basically the same
Reading
Writing
open a stream while more information write
information close the stream
open a stream while more information read
information close the stream
5Standard Input / Output Streams in Java
Writing to the screen System.out.print() System
.out.println()
Reading from the screen System.in.read(byte) S
ystem.in.read(byte,int,int)
- Standard streams are automatically opened, so
no need to - open them explicitly.
- Writing System.out.print(Hello\n) is the same
as writing - System.out.println(Hello)
- You have to create byte array before calling
read function. - read function has to be surrounded by try
catch(..) - block.
6Example - Echo
package test import java.io. public class
IOTest public IOTest() public static
void main(String args) byte ch new
byte80 int num 0 try
num System.in.read(ch)
System.out.println(new String(ch,0,num)) //
String(byte bt, int offset,int length)
catch (IOException ex)
System.out.println("Can't read")
ex.printStackTrace()
7java.io Classes Hierarchy
- The Streams can be divided into Character Streams
and Byte Streams - Character Streams
- Reader and Writer are the abstract superclasses
for character streams in java.io. Reader provides
the API and partial implementation for
readers--streams that read 16-bit characters--and
Writer provides the API and partial
implementation for writers--streams that write
16-bit characters. - Byte Streams
- To read and write 8-bit bytes, programs should
use the byte streams, descendants of InputStream
and OutputStream . InputStream and OutputStream
provide the API and partial implementation for
input streams (streams that read 8-bit bytes) and
output streams (streams that write 8-bit bytes).
These streams are typically used to read and
write binary data such as images and sounds.
8Character Streams Hierarchy
BufferedReader
LineNumberReader
CharArrayReader
InputStreamReader
FileReader
Reader
FilterReader
PushbackReader
PipedReader
StringReader
BufferedWriter
CharArrayWriter
OutputStreamWriter
FileWriter
Writer
FilterWriter
PipedWriter
StringWriter
9Example
import java.io. //must be imported public
class Copy public static void main(String
args) throws IOException File inputFile new
File("farrago.txt") File outputFile new
File("outagain.txt") //FileOutputStream and
FileInputStream for bytes //throws
FileNotFoundException (descendent of
IOException) FileReader in new
FileReader(inputFile) FileWriter out new
FileWriter(outputFile) int c
while ((c in.read()) ! -1)
out.write(c) // housekeeping in.close()
out.close()
10Advanced Usage
//extracts from a class not a whole
class//args ltname of filegtltname of
delimitergt import java.io. import
java.util.StringTokenizer . . . try int
lineNum, wordNum String line
BufferedReader inStream new BufferedReader(new
FileReader(args0)) lineNum wordNum
0 do line inStream.readLine()
if(line ! null)
lineNum StringTokenizer st new
StringTokenizer(line,args1) wordNum
st.countTokens()
while(line ! null) System.out.println("Th
ere are " lineNum " lines.")
System.out.println("There are " wordNum "
words.") catch(FileNotFoundException fnfe)
System.out.println("File (" args0 ") not
found.") catch(IOException ioe)
System.out.println("I/O error while reading file
(" args0 ")")
11 private void writeToFile(String from, String
to) try FileReader fr new
FileReader(from) BufferedReader br new
BufferedReader(fr) FileWriter fw new
FileWriter(to) String line null, srt
String wline int lineNum wordNum 0
while ( (line br.readLine()) ! null )
str line.split("\\s") wordNum
str.length lineNum
fw.write("There are " lineNum " lines.")
fw.write("There are " wordNum " words.")
fw.close() fr.close() catch
(IOException e) System.out.println("Uh oh,
got an IOException error!")
e.printStackTrace()
12IO - Advanced topics
- Redirecting standard IO
- System contains setIn, setOut and setErr.
- System.setIn(new FileInputStream(x.dat))
- System.setOut(new PrintStream(new
FileOutputStream( "out.txt"))) - Standard streams could be from/to anything you
want! - Compression
- Read about ZipInputStream and ZipOutputStream
- For more information
- jdk documentation
- On-line tutorials http//java.sun.com/docs/books
/tutorial/