JAVA Programming Language JAVA Input/Output 2
(1) (Stream) 2,,,,, 3 (2) InputStream OutputStream, 8 Reader Writer, 16 4
(3) Data Source/Dest. (byte stream) InputStream OutputStream Java Internal (byte, byte[ ]) < > Data Source/Dest. (byte stream) Character Encoding Reader Writer < > Unicode Java Internal (char, char[ ], String) 5 (4) OutputStream InputStream 6
(5) Reader Writer 7 (6) Reader BufferedReader LineNumberReader CharArrayReader InputStreamReader FileReader FilterReader PushbackReader PipedReader StringReader Writer / /, /, / / / / (pushback) PipedReader/PipedOutputStream InputStream BufferedInputStream LineNumberInputStream ByteArrayInputStream (none) FileInputStream FilterInputStream PushbackInputStream PipedInputStream StringBufferInputStream OutputStream BufferedWriter /, BufferedWriter BufferedOutputStream CharArrayWriter / / ByteArrayOutputStream FiterWriter / FilterOutputStream OutputStreamWriter FileWriter PrintWriter PipedWriter StringWriter / Writer/Streamdp PipedReader/PipedOutputStream (none) FileOutputStream PrintStream PipedOutputStream (none) 8
(1) InputStream, abstract int read( ) throws IOException int read(byte b[ ]) throws IOException int read(byte b[ ], int off, int len) throws IOException int available( ) throws IOException long skip(long n) throws IOException synchronized void mark(int readlimit) synchronized void reset( ) throws IOException void close( ) throws IOException 9 (2) InputStream import java.io.*; class InputStreamTest { public static void main(string args[ ]) { int ch; InputStream in = System.in; try { while((ch=in.read())!= -1) { if(ch == 'S') { in.skip(2); System.out.println("Char: "+(char)ch+", Available: "+in.available( )); catch(ioexception e) { System.out.println(e); 10
(3) OutputStream, abstract void write(int b) throws IOException void write(byte b[ ]) throws IOException void write(byte b[ ], int off, int len) throws IOException void flush( ) throws IOException void close( ) throws IOException 11 (4) OutputStream import java.io.*; class OutputStreamTest { public static void main(string args[ ]) { OutputStream out = System.out; int ch=0x0fffff00 + 'A'; try { out.write(ch); out.flush( ); // (a) // out.close( ); // (b) catch(ioexception e) { System.out.println(e); 12
(5) Reader, boolean ready( ); int read( ); int read(char[ ] cbuf); abstract int read(char[ ] cbuf, int off, int len); boolean marksupported( ); void mark(int readaheadlimit); void reset( ); long skip(long n); abstract void close( ); 13 (6) Writer, void write(int c ); void write(char[ ] cbuf); abstract void write(char[ ] cbuf, int off, int len); void write(string str); void write(string str, int off, int len); abstract void flush( ); abstract void close( ); 14
(1) FileInputStream FileInputStream FileInputStream // File FileInputStream(File file); // FileDescriptor FileInputStream(FileDescriptor fdobj); // FileInputStream(String name); 15 (2) FileOutputStream File FileOutputStream // File FileOutputStream(File file); // FileDescriptor FileOutputStream(FileDescriptor fdobj); // FileOutputStream(String name); // append FileOutputStream(String name, boolean append); 16
(3) import java.io.*; import java.lang.boolean; class FileInputOutputStreamTest { public staticvoid main(string[] args) { booleanappend = false ; int i, len=0; InputStream in=null; OutputStream out=null; if(args.length < 2) { System.out.println("USAGE: FileInputOutputStreamTest Source_file Destination_file"); System.exit(-1); else if(args.length > 2) { append = newboolean(args[2]).booleanvalue(); System.out.println("args[0]: "+args[0]); System.out.println("args[1]: "+args[1]); System.out.println(" append: "+append); try { in = newfileinputstream(newfile(args[0])); out = newfileoutputstream(args[1], append); catch(filenotfoundexception e) { System.out.println(e); try { while((i=in.read())!= -1) { out.write(i); len++; in.close(); out.close(); System.out.println(len+" bytes are copied..."); catch(ioexception e) { System.out.println(e); 17 (4) FileReader FileWriter // FileReader FileReader(File file); FileReader(FileDescriptor fd); FileReader(String filename); // FileWriter FileWriter(File file); FileWriter(FileDescriptor fd); FileWriter(String filename); FileWriter(String filename, boolean append); 18
(5) import java.io.*; import java.lang.boolean; class FileReaderWriterTest { public staticvoid main(string[] args) { booleanappend = false ; int i, len=0; FileReaderin=null; FileWriterout=null; if(args.length < 2) { System.out.println("USAGE: FileReaderWriterTest Source_file Destination_file"); System.exit(-1); else if(args.length > 2) { append = newboolean(args[2]).booleanvalue(); System.out.println("args[0]: "+args[0]); System.out.println("args[1]: "+args[1]); System.out.println(" append: "+append); try { in = newfilereader(newfile(args[0])); out = newfilewriter(args[1], append); catch(filenotfoundexception e) { System.out.println(e); catch(ioexception e) { System.out.println(e); try { while((i=in.read())!= -1) { out.write(i); len++; in.close(); out.close(); System.out.println(len+" bytes are copied..."); catch(ioexception e) { System.out.println(e); 19 (1) SequenceInputStream SequenceInputStream // Enumeration SequenceInputStream(Enumeration) // 2 SequenceInputStream(InputStream s1, InputStream s2) 20
(2) SequenceInputStream import java.io.*; class SequenceInputTest { public static void main(string[] args) throws IOException { int ch; byte arr1[] = { (byte )'H',(byte )'e',(byte )'l',(byte )'l',(byte )'o' ; byte arr2[] = { (byte )',',(byte )' ' ; ByteArrayInputStream in1 = new ByteArrayInputStream(arr1); ByteArrayInputStream in2 = new ByteArrayInputStream(arr2); FileInputStream in3 = new FileInputStream("SequenceInputTest.txt"); java.util.vector v = new java.util.vector(); v.add(in1); v.add(in2); v.add(in3); in3.reset(); SequenceInputStream in = new SequenceInputStream(v.elements()); while((ch=in.read())!= -1) { System.out.write(ch); System.out.flush(); System.out.println(); in1.reset(); if(in3.marksupported()) { System.out.println("reset()"); else { in3 = new FileInputStream("SequenceInputTest.txt"); System.out.println("newFileInputStream(\"SequenceInputTest.txt\")"); in = newsequenceinputstream(in1, in3); while((ch=in.read())!= -1) { System.out.print((char)ch); System.out.println(); 21 (3) PipedInputStream, PipedOutputStream 22
(4) PipedInputStream PipedOutputStream Java Program (Producer) PipedOutputStream PIPE PipedInputStream Java Program (Consumer) <Pipe > //PipedInputStream PipedInputStream( ); PipedInputStream(PipedOutputStream src); void connect(pipedoutputstream src); // PipedOutputStream PipedOutputStream( ); PipedOutputStream(PipedInputStream snk); void connect(pipedinputstream snk); 23 (5) PipedReaderStream PipedWriterStream //PipedReaderStream PipedReader( ); PipedReader(PipedWriter src); void connect(pipedwriter src); // PipedWriterStream PipedWriter( ); PipedWriter(PipedReader snk); void connect(pipedreader snk); 24
(6) PipedInputStream PipedOutputStream public PipedOutputStream getpipedoutput() { import java.io.*; class PipedInputThread extends Thread {... PipedInputThread () { public PipedInputStream getpipedinput() {......... public void connect(pipedoutputstream pout) { try { public void run() { class PipedOutputThread extends Thread {... PipedOutputThread() { pout = new PipedOutputStream();... public void connect(pipedinputstream pin) {... public void run() { try {... catch(ioexception e) { System.out. println(tostring() + ": "+e); public class PipedInputOutputTest { public static void main(string args[]) { PipedInputThread pin = null; PipedOutputThread pout = null; pin = new PipedInputThread(); pout = new PipedOutputThread(); pin.connect(pout. getpipedoutput()); pin.start(); pout.start(); 25 (1), ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter StringBufferInputStream StringReader StringWriter 26
(2) ByteArrayInputStream (protected byte[ ] buf) ByteArrayOutputStream // ByteArrayInputStream ByteArrayInputStream(byte[ ] buf); ByteArrayInputStream(byte[ ] buf, int offset, int length); // ByteArrayOutputStream ByteArrayOutputStream( ); ByteArrayOutputStream(int size); String tostring( ); String tostring(string enc); 27 (3) ByteArrayInputStream ByteArrayOutputStream import java.io.*; class ByteArrayInputOutputTest { public staticvoid main(string[] args) throws IOException { int ch; byte arr[] = { (byte)'j',(byte)'a',(byte)'v',(byte)'a',(byte)'!' ; ByteArrayInputStream in = new ByteArrayInputStream(arr); ByteArrayOutputStream out = newbytearrayoutputstream(); FileOutputStream outfile = newfileoutputstream("bytearrayinputoutputtest.txt"); while((ch=in.read())!= -1) { out.write(ch); System.out.println(" read: ["+(char)ch+"]" +", write: ["+out.tostring () +"]"+out.size()); System.out.println("String: "+out.tostring()); out.writeto(outfile); 28
(4) CharArrayReader CharArrayWriter // CharArrayReader CharArrayReader(char[ ] buf); CharArrayReader(char[ ] buf, int offset, int length); //CharArrayWriter CharArrayWriter( ); CharArrayWriter(int initialsize); int size( ); void writeto(write out);... 29 (5) CharArrayReader CharArrayWriter import java.io.*; class CharArrayReaderWriterTest { public static voidmain(string[ ] args) throws IOException { int ch; chararr[ ] = { 'H','e','l','l','o',',',' ','J','a','v','a','!' ; CharArrayReaderin; CharArrayWriter out; in = new CharArrayReader(arr, 7, 5); out = newchararraywriter( ); while((ch=in.read( ))!= -1) { out.write(ch); System.out.println("read: ["+(char)ch+"]"+", write: ["+out.tostring()+"]"+out.size( )); System.out.println(" String: "+out.tostring()); 30
(6) StringBufferInputStream 1. StringBufferInputStream // StringBufferInputStream(String s); 31 (7) StringBufferInputStream import java.io.*; class StringBufferInputTest { public static void main(string[ ] args) throws IOException { int ch; String s = "Hi~"; StringBufferInputStream in = new StringBufferInputStream(s); while((ch=in.read())!= -1) { System.out.println("read: ["+(char)ch+","+ch+"]"+in.available( )); 32
(8) StringReader StringWriter // StringReader StringReader(String s); // StringWriter StringWriter( ); StringWriter(int initialsize); StringBuffer getbuffer( ); String tostring( ); 33 (9) StringReader StringWriter import java.io.*; class StringReaderWriterTest{ publicstatic void main(string[]args) throws IOException { int ch; String s = "Hello, Java!"; StringReader in = newstringreader(s); StringWriter out = newstringwriter(s.length()/2); in.skip(7); while((ch=in.read())!= -1) { out.write(ch); System.out.println("read: ["+(char)ch+"]" +", write: ["+out.tostring()+"]" +out.getbuffer().length()); System.out.println(" out: "+out.tostring()); System.out.println(" out: "+out.getbuffer().reverse()); in = newstringreader(out.tostring()); CharArrayWriterout2 = newchararraywriter(); while((ch=in.read())!= -1) { out2.write(ch); System.out.println("read: ["+(char)ch+"]"+", write: ["+out2.tostring()+"]"+out2.size() out2.writeto(out); System.out.println(" out: "+out.tostring()); System.out.println("out2: "+out2.tostring()); 34
(1) 1. : FilterInputStream / FilterOutputStream FilterReader / FilterWriter 2. : BufferedInputStream / BufferedOutputStream, BufferedReader / BufferedWriter 3. : DataInputStream / DataOutputStream 4. : LineNumberInputStream / LineNumberReader 5. : PushbackInputStream / PushbackReader 6. : PrintStream / PrintWriter Java Program FilterInputStream FilterOutputStream InputStream OutputStream <Filter > Data Source / Destination 35 (2) FilterInputStream FilterOutputStream FilterReader FilterWriter 36
(3) BufferedInputStream mark reset BufferedOutputStream BufferedReader BufferedWriter 37 (4) DataInputStream, DataOutputStream 38
(5) LineNumberInputStream JDK1.1 LineNumberReader LineNumberReader LineNumberInputStream 39 (5) import java.io.*; class LineNumberInputTest { public static voidmain(string[]args) { int ch, i=-1; LineNumberInputStream in=null; try { FileInputStream fin = newfileinputstream("test.java"); in = new LineNumberInputStream(fin); catch(filenotfoundexceptione) { System.out.println(e); try { while((ch=in.read())!= -1) { if(i!= in.getlinenumber()) { i = in.getlinenumber(); if(i >= 10) { in.setlinenumber(0); i = in.getlinenumber(); System.out.print("currline: "+i+", "); System.out.println(); catch(ioexceptione) { System.out.println(e); 40
(6) PushbackInputStream PushbackReader 41 (6) import java.io.*; class PushbackInputTest { public static int readnum(pushbackinputstream in) { int i, ch; i=0; try { while(((ch=in.read())!= -1)&((ch >= '0')&&(ch <= '9'))) { i = (i * 10) + (ch - '0'); in.unread(ch); catch(ioexception e) { System.out.println(e); return(i); 42
(6) ( ) public static voidmain(string[ ] args) { int ch, i=-1; int op1, op2; charopr='\u0000'; PushbackInputStream in=new PushbackInputStream(System.in); op1 = readnum (in); try { opr = (char)in.read( ); catch(ioexception e) { System.out.println(e); op2 = readnum (in); switch(opr) { case '+': System.out.println(op1+" "+opr+" "+op2+" = "+(op1+op2)); break; case '-': System.out.println(op1+" "+opr+" "+op2+" = "+(op1-op2)); break; case '*': System.out.println(op1+" "+opr+" "+op2+" = "+(op1*op2)); break; case '/': System.out.println(op1+" "+opr+" "+op2+" = "+(op1/op2)); break; 43. ObjectInputStream ObjectOutputStream 44
ObjectInputStream / ObjectOutputStream, ObjectInput / ObjectOutput 45 ObjectInput DataInout // ObjectInput int available( ); int read( ); int read(byte[ ] b); // //. //. int read(byte[ ] b, int off, int len); //. Object readobject( ); //. long skip(long n); void close( ); //. //. 46
ObjectOutput DataOutput // ObjectOutput void write(byte[ ] b); void write(byte[ ] b, int off, int len); void write(int b); // // void writeobject(object obj); // void flush( ); void close( ); // flush //. // 47 ObjectInputStream ObjectOutputStream // ObjectInputStream FileInputStream istream = new FileInputStream( t.tmp ); ObjectInputStream p = new ObjectInputStream(istream); int i = p.readint( ); String today = (String)p.readObject( ); Date data = (Date)p.readObject( ); istream.close( ); 48
ObjectOutputStream // ObjectOutputStream FileOutputStream ostream = new FileOutputStream( t.tmp ); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeint(12345); p.writeobject( Today ); p.writeobject(new Date( )); p.flush( ); ostream.close( ); 49 Serializable. Serializable wirteobject readobject Externalizable Serializable writeexternal readexternal 50
import java.io.*; class ObjectData implements Serializable { String name; int age; float height; private void readobject(objectinputstream stream) throws IOException, ClassNotFoundException { stream.defaultreadobject(); System.out.print(" readobject: "); public ObjectData () { this("noname", 0, 0.0f); public ObjectData (String name, int age, float height) { this.name = name; this.age = age; this.height = height; public String tostring() { return("name: "+name+", age: "+age+", height: " + height); class ObjectInputOutputTest { public static void main(string[] args) { ObjectData [] data = new ObjectData [3]; private void writeobject(objectoutputstream stream) throws IOException { stream.defaultwriteobject(); System.out.print("writeObject: "); data[0] = new ObjectData ("Yongwoo", 26, 168f); data[1] = new ObjectData ("Kildong ", 19, 174.8f); data[2] = new ObjectData ("Dongsoo ", 30, 169.4f); try { FileOutputStream outf = new FileOutputStream("ObjectInputOutputTest.txt"); ObjectOutput out = newobjectoutputstream(outf); 51 ( ) out.writeint(data.length); System.out.println("Number: "+data.length); for(int i=0;i<data.length;i++) { out.writeobject(data[i]); System.out.println(data[i]); out.close(); FileInputStream inf = new FileInputStream("ObjectInputOutputTest.txt"); ObjectInput in = new ObjectInputStream(inf); int numberofdata ; ObjectData objdata ; numberofdata = in.readint(); System.out.println("Number: "+numberofdata ); for(int i=0;i<numberofdata ;i++) { objdata = (ObjectData )in.readobject(); System.out.println(objData); in.close(); catch(exception e) { e.printstacktrace(); 52
RandomAccessFile,.., RandomAccessFile 53 RandomAccessFile // RandomAccessFile RandomAccessFile(String name, String mode); RandomAccessFile(File file, String mode); void seek(long pos); // long getfilepointer( ); // long length( ); // 54
RandomAccessFile import java.io.*; publicclass RandomAccessFileTest { public staticvoid main(string[ ] args)throws IOException { RandomAccessFile f; f = newrandomaccessfile("randomaccessfile.txt", "rw"); for(int i=0;i<100;i++) { f.writeint(i); for(int i=0;i<10;i++) { int accpointer= (int)(math.random( )*100); f.seek(accpointer* 4); System.out.print("Current FP: " + f.getfilepointer( )); System.out.println(", Data: " + f.readint( )); f.close(); C:\>javac RandomAccessFileTest.java C:\>java RandomAccessFileTest Current FP: 20, Data: 5 Current FP: 72, Data: 18 Current FP: 256, Data: 64 Current FP: 104, Data: 26 Current FP: 304, Data: 76 Current FP: 40, Data: 10 Current FP: 96, Data: 24 Current FP: 168, Data: 42 Current FP: 96, Data: 24 Current FP: 324, Data: 81 C:\> 55