Java IO. Streams

download Java IO. Streams

If you can't read please download the document

description

Presentation about java streams. Byte streams, character streams and etc.

Transcript of Java IO. Streams

  • 1. Java java.io

2.

    • , ( ), ( )

3. java.io

  • java.io
    • InputStream / OutputStream
    • FileInputStream / FileOutputStream
    • BufferedInputStream / BufferedOutputStream
    • InputStreamReader / OutputStreamWriter
    • FileReader / FileWriter
    • BufferedReader / BufferedWriter
    • StringReader / StringWriter
    • DataInputStream / DataOutputStream
    • ObjectInputStream/ ObjectOutputStream

4. FileInputStream

  • class FileInputStream extends InputStream
  • public FileInputStream(String name) throws FileNotFoundException
  • public FileInputStream(File file) throws FileNotFoundException
  • public FileInputStream(FileDescriptor fdObj)
  • public native int read() throws IOException
  • private native int readBytes(byte b[], int off, int len) throws IOException
  • public int read(byte b[]) throws IOException
  • public int read(byte b[], int off, int len) throws IOException
  • public native long skip(long n) throws IOException
  • public native int available() throws IOException
  • public void close() throws IOException
  • public FileChannel getChannel()
  • public boolean markSupported()
  • public synchronized void reset() throws IOException
  • public synchronized void mark(int readlimit)

5. FileOutputStream

  • class FileOutputStream extends OutputStream
  • public FileOutputStream(String name) throwsFileNotFoundException
  • public FileOutputStream(String name, boolean append)throws FileNotFoundException
  • public FileOutputStream(File file) throws FileNotFoundException
  • public FileOutputStream(File file, boolean append) throws FileNotFoundException
  • public FileOutputStream(FileDescriptor fdObj)
  • public native void write(int b) throws IOException
  • public void write(byte b[]) throws IOException
  • public void write(byte b[], int off, int len) throws IOException
  • public void close() throws IOException
  • public void flush() throws IOException

6. ( read() write(int))

  • try{ // try try{ // fo=new FileOutputStream("out.txt"); int l=s.length(); int i=0; while(i

7. ()

  • try{ // fi=new FileInputStream("out.txt"); int i=0; StringBuilder sb=new StringBuilder(); while((i=fi.read())!=-1) { sb.append(Character.toChars(i)); } System.out.println("Received from file: "+sb.toString()); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // try { System.out.println(e); }catch(IOException e) // try { System.out.println(e); }

8. ( read(byte[]) write(byte[]))

  • try{// try try{ // fo=new FileOutputStream("out.txt"); int l=s.length(); int i=0; byte[] b=s.getBytes(); fo.write(b); }finally{ if(fo!=null) fo.close(); }

9. ()

  • try{ // fi=new FileInputStream("out.txt"); int i=fi.available(); byte[] b=new byte[i]; fi.read(b); String s1=new String(b); System.out.println("Received from file: "+s1); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // try { System.out.println(e); }catch(IOException e) // try { System.out.println(e); }

10. ( read(byte[], int, int) write(byte[], int , int))

  • try{ // try try{ // fo=new FileOutputStream("out.txt"); int l=s.length(); int i=0; byte[] b=s.getBytes(); fo.write(b,0,b.length); }finally{ if(fo!=null) fo.close(); }

11. ()

  • try{// fi=new FileInputStream("out.txt"); int i=fi.available(); byte[] b=new byte[i]; fi.read(b,0,i); String s1=new String(b); System.out.println("Received from file: "+s1); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // try { System.out.println(e); }catch(IOException e) // try { System.out.println(e); }

12. BufferedInputStream

  • class BufferedInputStream extends FilterInputStream
  • public BufferedInputStream(InputStream in)
  • public BufferedInputStream(InputStream in, int size)
  • public synchronized int read() throws IOException
  • public synchronized int read(byte b[], int off, int len) throws IOException
  • public synchronized long skip(long n) throws IOException
  • public synchronized int available() throws IOException
  • public synchronized void mark(int readlimit)
  • public synchronized void reset() throws IOException
  • public boolean markSupported()
  • public void close() throws IOException

13. BufferedOutputStream

  • class BufferedOutputStream extends FilterOutputStream
  • public BufferedOutputStream(OutputStream out)
  • public BufferedOutputStream(OutputStream out, int size)
  • public synchronized void write(int b) throws IOException
  • public synchronized void write(byte b[], int off, int len) throws IOException
  • public synchronized void flush() throws IOException

14. BufferedInputStream/BufferedOutputStream

  • try{ // try try{ // bo=new BufferedOutputStream(new FileOutputStream("out.txt")); byte[] b=new byte[s.length()]; b=s.getBytes(); bo.write(b,0,b.length); }finally{ if(bo!=null) bo.close(); }

15. ()

  • try{// bi=new BufferedInputStream(new FileInputStream("out.txt")); int i=bi.available(); byte[] b=new byte[i]; bi.mark(i); bi.read(b, 0, i); bi.reset(); String s1=new String(b); bi.read(b, 0, i); String s2=new String(b); System.out.println(s1+" "+s2); }finally{ if(bi!=null) bi.close(); } }catch(Exception e){ // try System.out.println(e); }

16. InputStreamReader

  • public class InputStreamReader extends Reader
  • public InputStreamReader(InputStream in)
  • public InputStreamReader(InputStream in, String charsetName)
  • public InputStreamReader(InputStream in, Charset cs)
  • public InputStreamReader(InputStream in, CharsetDecoder dec)
  • public String getEncoding()
  • public int read() throws IOException
  • public int read(char cbuf[]) throws IOException
  • public int read(char cbuf[], int offset, int length) throws IOException
  • public boolean ready() throws IOException
  • public void close() throws IOException

17. OutputStreamWriter

  • class OutputStreamWriter extends Writer
  • public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException
  • public OutputStreamWriter(OutputStream out)
  • public OutputStreamWriter(OutputStream out, Charset cs)
  • public OutputStreamWriter(OutputStream out, CharsetEncoder enc)
  • public String getEncoding()
  • public void write(int c) throws IOException
  • public void write(char cbuf[], int off, int len) throws IOException
  • public void write(String str) throws IOException
  • public void write(String str, int off, int len) throws IOException
  • public void flush() throws IOException
  • public void close() throws IOException

18. OutputStreamWriter/InputStreamReader

  • try{ // try try{ // or=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("out.txt")),"KOI8-R"); or.write(s); }finally{ if(or!=null) or.close(); }

19. ()

  • try{ // ir=new InputStreamReader(new BufferedInputStream(new FileInputStream("out.txt")),"KOI8-R"); char[] b=new char[1024]; int i=0; StringBuilder sb=new StringBuilder(); while((i=ir.read(b))!=-1) { sb.append(b, 0, i); } System.out.println("Result: "+sb.toString()); }finally{ if(ir!=null) ir.close(); } }catch(Exception e) // try { System.out.println(e); }

20. FileReader

  • class FileReader extends InputStreamReader
  • public FileReader(String fileName) throws FileNotFoundException
  • public FileReader(File file) throws FileNotFoundException
  • public FileReader(FileDescriptor fd)

21. FileWriter

  • class FileWriter extends OutputStreamWriter
  • public FileWriter(String fileName) throws IOException
  • public FileWriter(String fileName, boolean append) throws IOException
  • public FileWriter(File file) throws IOException
  • public FileWriter(File file, boolean append) throws IOException
  • public FileWriter(FileDescriptor fd)

22. FileReader FileWriter

  • try{// try try{ // fw=new FileWriter("out.txt");fw.write(s);
  • }finally{
  • if(fw!=null)
  • fw.close();
  • }

23. ()

  • try{
  • fr=new FileReader("out.txt");
  • char[] b=new char[1024];
  • int i=0;
  • StringBuilder sb=new StringBuilder();
  • while((i=fr.read(b))!=-1)
  • {
  • sb.append(b, 0, i);
  • }
  • System.out.println(sb.toString());
  • }finally{
  • if(fr!=null)
  • fr.close();
  • }
  • }catch(Exception e)
  • {
  • System.out.println(e);
  • }

24. BufferedReader

  • class BufferedReader extends Reader
  • public BufferedReader(Reader in, int sz)
  • public BufferedReader(Reader in)
  • public int read() throws IOException
  • public int read(char cbuf[], int off, int len) throws IOException
  • String readLine(boolean ignoreLF) throws IOException
  • public String readLine() throws IOException
  • public long skip(long n) throws IOException
  • public boolean ready() throws IOException
  • public boolean markSupported()
  • public void mark(int readAheadLimit) throws IOException
  • public void reset() throws IOException
  • public void close() throws IOException

25. BufferedWriter

  • class BufferedWriter extends Writer
  • public BufferedWriter(Writer out)
  • public BufferedWriter(Writer out, int sz)
  • public void write(int c) throws IOException
  • public void write(char cbuf[], int off, int len) throws IOException
  • public void write(String s, int off, int len) throws IOException
  • public void newLine() throws IOException
  • public void flush() throws IOException
  • public void close() throws IOException

26. BufferedReader/BufferedWriter

  • try{ // try try{ // bw=new BufferedWriter(new FileWriter("out.txt")); bw.write(s);}finally{ if(bw!=null) bw.close(); } try{ // br=new BufferedReader(new FileReader("out.txt")); String s1=br.readLine(); System.out.println(s1); }finally{ if(br!=null) br.close(); } }catch(Exception e) // try { System.out.println(e); }

27. DataInputStream

  • class DataInputStream extends FilterInputStream implements DataInput
  • public DataInputStream(InputStream in)
  • void readFully(byte b[]) throws IOException
  • void readFully(byte b[], int off, int len) throws IOException
  • int skipBytes(int n) throws IOException
  • boolean readBoolean() throws IOException
  • byte readByte() throws IOException
  • int readUnsignedByte() throws IOException
  • short readShort() throws IOException
  • int readUnsignedShort() throws IOException
  • char readChar() throws IOException
  • int readInt() throws IOException
  • long readLong() throws IOException
  • float readFloat() throws IOException
  • double readDouble() throws IOException
  • String readLine() throws IOException

28. DataOutputStream

  • class DataOutputStream extends FilterOutputStream implements DataOutput
  • public DataOutputStream(OutputStream out)
  • void write(int b) throws IOException
  • void write(byte b[]) throws IOException
  • void write(byte b[], int off, int len) throws IOException
  • void writeBoolean(boolean v) throws IOException
  • void writeByte(int v) throws IOException
  • void writeShort(int v) throws IOException
  • void writeChar(int v) throws IOException
  • void writeInt(int v) throws IOException
  • void writeLong(long v) throws IOException
  • void writeFloat(float v) throws IOException
  • void writeDouble(double v) throws IOException
  • void writeBytes(String s) throws IOException
  • void writeChars(String s) throws IOException

29. DataInputStream/DataOutputStream

  • int i=10; char c='a'; boolean b=true; float f=10.6f; String s="Hello"; try{ try{ dout=new DataOutputStream(new FileOutputStream("out2.txt")); dout.writeInt(i); dout.writeChar(c); dout.writeBoolean(b); dout.writeFloat(f); dout.writeUTF(s); }finally{ if(dout!=null) dout.close(); }

30. ()

  • try{ di=new DataInputStream(new FileInputStream("out2.txt")); int j=di.readInt(); char c1=di.readChar(); boolean b1=di.readBoolean(); float f1=di.readFloat(); String s1=di.readUTF(); System.out.println("Int: "+j+" char: "+c1+" bool: "+b1+" float: "+f1+" Str: "+s1); }finally{ if(di!=null) di.close(); }}catch(Exception e) { System.out.println(e); }

31. ObjectInputStream

  • class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
  • public ObjectInputStream(InputStream in) throws IOException
  • public final Object readObject()throws IOException, ClassNotFoundException

32. ObjectOutputStream

  • class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants
  • public ObjectOutputStream(OutputStream out) throws IOException
  • public final void writeObject(Object obj) throws IOException

33. ObjectInputStream/ObjectOutputStream

  • TestClass tc=new TestClass(10,"Hello, world"); System.out.println(tc); try{ try{ oo=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("out3.txt"))); oo.writeObject(tc); }finally{ if(oo!=null)oo.close(); } try{ oi=new ObjectInputStream(new BufferedInputStream(new FileInputStream("out3.txt"))); TestClass ttt=(TestClass)oi.readObject(); System.out.println(ttt); }finally{ if(oi!=null)oi.close(); } }catch(Exception e){ System.out.println(e); }

34. e-mail: [email protected]