Your SlideShare is downloading. ×
0

      
       Язык Java 
      
     
      
       Потоки 
       java.io 
      
     
      
       Алексей Бованенко ...

      
       Введение 
      
     
      
       <ul>
        
         <li>Понятие потоков 
        
       </ul>
    ...

      
       Потоки в java.io 
      
     
      
       <ul>
        
         <li>В java.io определены следующие пото...

      
       FileInputStream 
      
     
      
       <ul>
        
         <li>class FileInputStream extends InputS...

      
       FileOutputStream 
      
     
      
       <ul>
        
         <li>class FileOutputStream extends Outp...

      
       Пример использования (использование методов read() и write(int)) 
      
     
      
       <ul>
        
...

      
       Пример (продолжение) 
      
     
      
       <ul>
        
         <li>try{ //  Создание входного пото...

      
       Пример использования (использование методов read(byte[]) и write(byte[])) 
      
     
      
       <ul>
...

      
       Пример (продолжение) 
      
     
      
       <ul>
        
         <li>try{ //  Создание входного пото...

      
       Пример использования (использование методов read(byte[], int, int) и write(byte[], int , int)) 
      
    ...

      
       Пример (продолжение) 
      
     
      
       <ul>
        
         <li>try{  //  Создание входного пот...

      
       BufferedInputStream 
      
     
      
       <ul>
        
         <li>class BufferedInputStream extend...

      
       BufferedOutputStream 
      
     
      
       <ul>
        
         <li>class BufferedOutputStream exte...

      
       Пример использования  BufferedInputStream/BufferedOutputStream 
      
     
      
       <ul>
        
  ...

      
       Пример использования  (продолжение) 
      
     
      
       <ul>
        
         <li>try{  //  Создан...

      
       InputStreamReader 
      
     
      
       <ul>
        
         <li>public class InputStreamReader ext...

      
       OutputStreamWriter 
      
     
      
       <ul>
        
         <li>class OutputStreamWriter extends ...

      
       Пример использования  OutputStreamWriter/InputStreamReader 
      
     
      
       <ul>
        
      ...

      
       Пример использования  (продолжение) 
      
     
      
       <ul>
        
         <li>try{ //  Создани...

      
       FileReader 
      
     
      
       <ul>
        
         <li>class FileReader extends InputStreamReade...

      
       FileWriter 
      
     
      
       <ul>
        
         <li>class FileWriter extends OutputStreamWrit...

      
       Пример использования  FileReader и FileWriter 
      
     
      
       <ul>
        
         <li>try{  ...

      
       Пример использования (продолжение) 
      
     
      
       <ul>
        
         <li>try{ 
        
  ...

      
       BufferedReader 
      
     
      
       <ul>
        
         <li>class BufferedReader extends Reader 
...

      
       BufferedWriter 
      
     
      
       <ul>
        
         <li>class BufferedWriter extends Writer 
...

      
       Пример использования   BufferedReader/BufferedWriter 
      
     
      
       <ul>
        
         <li...

      
       DataInputStream 
      
     
      
       <ul>
        
         <li>class DataInputStream extends Filter...

      
       DataOutputStream 
      
     
      
       <ul>
        
         <li>class DataOutputStream extends Filt...

      
       Пример использования DataInputStream/DataOutputStream 
      
     
      
       <ul>
        
         <l...

      
       Пример использования (продолжение) 
      
     
      
       <ul>
        
         <li>try{   di=new Dat...

      
       ObjectInputStream 
      
     
      
       <ul>
        
         <li>class ObjectInputStream extends In...

      
       ObjectOutputStream 
      
     
      
       <ul>
        
         <li>class ObjectOutputStream extends ...

      
       Пример использования ObjectInputStream/ObjectOutputStream 
      
     
      
       <ul>
        
       ...

      
       Конец 
      
     
      
       Вопросы 
       e-mail: a.bovanenko@gmail.com
Upcoming SlideShare
Loading in...5
×

Java IO. Streams

1,965

Published on

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

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,965
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Java IO. Streams"

  1. 1. Язык Java Потоки java.io Алексей Бованенко
  2. 2. Введение <ul> <li>Понятие потоков </ul> <ul> <ul> <li>Поток представляет собой набор данных, связанный с источником (поток ввода), либо приемником (поток вывода) </ul> </ul> <ul> <ul> <li>Источник или приемник </ul> </ul> <ul> <ul> <ul> <li>Файлы на диске </ul> </ul> </ul> <ul> <ul> <ul> <li>Устройства </ul> </ul> </ul> <ul> <ul> <ul> <li>Другие программы </ul> </ul> </ul> <ul> <ul> <ul> <li>Буфер в памяти </ul> </ul> </ul> <ul> <ul> <li>Потоки могут содержать данные различных типов </ul> </ul> <ul> <ul> <ul> <li>Примитивные типы </ul> </ul> </ul> <ul> <ul> <ul> <li>Символы </ul> </ul> </ul> <ul> <ul> <ul> <li>Объектные потоки </ul> </ul> </ul>
  3. 3. Потоки в java.io <ul> <li>В java.io определены следующие потоки </ul> <ul> <ul> <li>InputStream / OutputStream </ul> </ul> <ul> <ul> <li>FileInputStream / FileOutputStream </ul> </ul> <ul> <ul> <li>BufferedInputStream / BufferedOutputStream </ul> </ul> <ul> <ul> <li>InputStreamReader / OutputStreamWriter </ul> </ul> <ul> <ul> <li>FileReader / FileWriter </ul> </ul> <ul> <ul> <li>BufferedReader / BufferedWriter </ul> </ul> <ul> <ul> <li>StringReader / StringWriter </ul> </ul> <ul> <ul> <li>DataInputStream / DataOutputStream </ul> </ul> <ul> <ul> <li>ObjectInputStream / ObjectOutputStream </ul> </ul>
  4. 4. FileInputStream <ul> <li>class FileInputStream extends InputStream </ul> <ul> <li>public FileInputStream(String name) throws FileNotFoundException </ul> <ul> <li>public FileInputStream(File file) throws FileNotFoundException </ul> <ul> <li>public FileInputStream(FileDescriptor fdObj) </ul> <ul> <li>public native int read() throws IOException </ul> <ul> <li>private native int readBytes(byte b[], int off, int len) throws IOException </ul> <ul> <li>public int read(byte b[]) throws IOException </ul> <ul> <li>public int read(byte b[], int off, int len) throws IOException </ul> <ul> <li>public native long skip(long n) throws IOException </ul> <ul> <li>public native int available() throws IOException </ul> <ul> <li>public void close() throws IOException </ul> <ul> <li>public FileChannel getChannel() </ul> <ul> <li>public boolean markSupported() </ul> <ul> <li>public synchronized void reset() throws IOException </ul> <ul> <li>public synchronized void mark(int readlimit) </ul>
  5. 5. FileOutputStream <ul> <li>class FileOutputStream extends OutputStream </ul> <ul> <li>public FileOutputStream(String name) throws FileNotFoundException </ul> <ul> <li>public FileOutputStream(String name, boolean append) throws FileNotFoundException </ul> <ul> <li>public FileOutputStream(File file) throws FileNotFoundException </ul> <ul> <li>public FileOutputStream(File file, boolean append) throws FileNotFoundException </ul> <ul> <li>public FileOutputStream(FileDescriptor fdObj) </ul> <ul> <li>public native void write(int b) throws IOException </ul> <ul> <li>public void write(byte b[]) throws IOException </ul> <ul> <li>public void write(byte b[], int off, int len) throws IOException </ul> <ul> <li>public void close() throws IOException </ul> <ul> <li>public void flush() throws IOException </ul>
  6. 6. Пример использования (использование методов read() и write(int)) <ul> <li>try{ // Внешний блок try try{ // Создание потока вывода fo=new FileOutputStream(&quot;out.txt&quot;); int l=s.length(); int i=0; while(i<l) { fo.write(s.codePointAt(i++)); } }finally{ if(fo!=null) fo.close(); } </ul>
  7. 7. Пример (продолжение) <ul> <li>try{ // Создание входного потока fi=new FileInputStream(&quot;out.txt&quot;); int i=0; StringBuilder sb=new StringBuilder(); while((i=fi.read())!=-1) { sb.append(Character.toChars(i)); } System.out.println(&quot;Received from file: &quot;+sb.toString()); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // Перехват для внешнего try { System.out.println(e); }catch(IOException e) // Перехват для внешнего try { System.out.println(e); } </ul>
  8. 8. Пример использования (использование методов read(byte[]) и write(byte[])) <ul> <li>try{ // Внешний блок try try{ // Создание потока вывода fo=new FileOutputStream(&quot;out.txt&quot;); int l=s.length(); int i=0; byte[] b=s.getBytes(); fo.write(b); }finally{ if(fo!=null) fo.close(); } </ul>
  9. 9. Пример (продолжение) <ul> <li>try{ // Создание входного потока fi=new FileInputStream(&quot;out.txt&quot;); int i=fi.available(); byte[] b=new byte[i]; fi.read(b); String s1=new String(b); System.out.println(&quot;Received from file: &quot;+s1); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // Перехват для внешнего try { System.out.println(e); }catch(IOException e) // Перехват для внешнего try { System.out.println(e); } </ul>
  10. 10. Пример использования (использование методов read(byte[], int, int) и write(byte[], int , int)) <ul> <li>try{ // Внешний блок try try{ // Создание потока вывода fo=new FileOutputStream(&quot;out.txt&quot;); int l=s.length(); int i=0; byte[] b=s.getBytes(); fo.write(b,0,b.length); }finally{ if(fo!=null) fo.close(); } </ul>
  11. 11. Пример (продолжение) <ul> <li>try{ // Создание входного потока fi=new FileInputStream(&quot;out.txt&quot;); int i=fi.available(); byte[] b=new byte[i]; fi.read(b,0,i); String s1=new String(b); System.out.println(&quot;Received from file: &quot;+s1); }finally{ if(fi!=null) fi.close(); } }catch(FileNotFoundException e) // Перехват для внешнего try { System.out.println(e); }catch(IOException e) // Перехват для внешнего try { System.out.println(e); } </ul>
  12. 12. BufferedInputStream <ul> <li>class BufferedInputStream extends FilterInputStream </ul> <ul> <li>public BufferedInputStream(InputStream in) </ul> <ul> <li>public BufferedInputStream(InputStream in, int size) </ul> <ul> <li>public synchronized int read() throws IOException </ul> <ul> <li>public synchronized int read(byte b[], int off, int len) throws IOException </ul> <ul> <li>public synchronized long skip(long n) throws IOException </ul> <ul> <li>public synchronized int available() throws IOException </ul> <ul> <li>public synchronized void mark(int readlimit) </ul> <ul> <li>public synchronized void reset() throws IOException </ul> <ul> <li>public boolean markSupported() </ul> <ul> <li>public void close() throws IOException </ul>
  13. 13. BufferedOutputStream <ul> <li>class BufferedOutputStream extends FilterOutputStream </ul> <ul> <li>public BufferedOutputStream(OutputStream out) </ul> <ul> <li>public BufferedOutputStream(OutputStream out, int size) </ul> <ul> <li>public synchronized void write(int b) throws IOException </ul> <ul> <li>public synchronized void write(byte b[], int off, int len) throws IOException </ul> <ul> <li>public synchronized void flush() throws IOException </ul>
  14. 14. Пример использования BufferedInputStream/BufferedOutputStream <ul> <li>try{ // Внешний блок try try{ // Создание потока для записи файла bo=new BufferedOutputStream(new FileOutputStream(&quot;out.txt&quot;)); byte[] b=new byte[s.length()]; b=s.getBytes(); bo.write(b,0,b.length); }finally{ if(bo!=null) bo.close(); } </ul>
  15. 15. Пример использования (продолжение) <ul> <li>try{ // Создание потока для чтения bi=new BufferedInputStream(new FileInputStream(&quot;out.txt&quot;)); 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+&quot; &quot;+s2); }finally{ if(bi!=null) bi.close(); } }catch(Exception e){ // Перехват для внешнего try System.out.println(e); } </ul>
  16. 16. InputStreamReader <ul> <li>public class InputStreamReader extends Reader </ul> <ul> <li>public InputStreamReader(InputStream in) </ul> <ul> <li>public InputStreamReader(InputStream in, String charsetName) </ul> <ul> <li>public InputStreamReader(InputStream in, Charset cs) </ul> <ul> <li>public InputStreamReader(InputStream in, CharsetDecoder dec) </ul> <ul> <li>public String getEncoding() </ul> <ul> <li>public int read() throws IOException </ul> <ul> <li>public int read(char cbuf[]) throws IOException </ul> <ul> <li>public int read(char cbuf[], int offset, int length) throws IOException </ul> <ul> <li>public boolean ready() throws IOException </ul> <ul> <li>public void close() throws IOException </ul>
  17. 17. OutputStreamWriter <ul> <li>class OutputStreamWriter extends Writer </ul> <ul> <li>public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException </ul> <ul> <li>public OutputStreamWriter(OutputStream out) </ul> <ul> <li>public OutputStreamWriter(OutputStream out, Charset cs) </ul> <ul> <li>public OutputStreamWriter(OutputStream out, CharsetEncoder enc) </ul> <ul> <li>public String getEncoding() </ul> <ul> <li>public void write(int c) throws IOException </ul> <ul> <li>public void write(char cbuf[], int off, int len) throws IOException </ul> <ul> <li>public void write(String str) throws IOException </ul> <ul> <li>public void write(String str, int off, int len) throws IOException </ul> <ul> <li>public void flush() throws IOException </ul> <ul> <li>public void close() throws IOException </ul>
  18. 18. Пример использования OutputStreamWriter/InputStreamReader <ul> <li>try{ // Внешний блок try try{ // Создания потока для записи данных or=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(&quot;out.txt&quot;)),&quot;KOI8-R&quot;); or.write(s); }finally{ if(or!=null) or.close(); } </ul>
  19. 19. Пример использования (продолжение) <ul> <li>try{ // Создание потока для чтения данных ir=new InputStreamReader(new BufferedInputStream(new FileInputStream(&quot;out.txt&quot;)),&quot;KOI8-R&quot;); 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(&quot;Result: &quot;+sb.toString()); }finally{ if(ir!=null) ir.close(); } }catch(Exception e) // Перехват для внешнего try { System.out.println(e); } </ul>
  20. 20. FileReader <ul> <li>class FileReader extends InputStreamReader </ul> <ul> <li>public FileReader(String fileName) throws FileNotFoundException </ul> <ul> <li>public FileReader(File file) throws FileNotFoundException </ul> <ul> <li>public FileReader(FileDescriptor fd) </ul>
  21. 21. FileWriter <ul> <li>class FileWriter extends OutputStreamWriter </ul> <ul> <li>public FileWriter(String fileName) throws IOException </ul> <ul> <li>public FileWriter(String fileName, boolean append) throws IOException </ul> <ul> <li>public FileWriter(File file) throws IOException </ul> <ul> <li>public FileWriter(File file, boolean append) throws IOException </ul> <ul> <li>public FileWriter(FileDescriptor fd) </ul>
  22. 22. Пример использования FileReader и FileWriter <ul> <li>try{ // Внешний блок try try{ // Создание файла fw=new FileWriter(&quot;out.txt&quot;); fw.write(s); </ul> <ul> <li>}finally{ </ul> <ul> <li>if(fw!=null) </ul> <ul> <li>fw.close(); </ul> <ul> <li>} </ul> <ul> </ul>
  23. 23. Пример использования (продолжение) <ul> <li>try{ </ul> <ul> <li>fr=new FileReader(&quot;out.txt&quot;); </ul> <ul> <li>char[] b=new char[1024]; </ul> <ul> <li>int i=0; </ul> <ul> <li>StringBuilder sb=new StringBuilder(); </ul> <ul> <li>while((i=fr.read(b))!=-1) </ul> <ul> <li>{ </ul> <ul> <li>sb.append(b, 0, i); </ul> <ul> <li>} </ul> <ul> <li>System.out.println(sb.toString()); </ul> <ul> <li>}finally{ </ul> <ul> <li>if(fr!=null) </ul> <ul> <li>fr.close(); </ul> <ul> <li>} </ul> <ul> <li>}catch(Exception e) </ul> <ul> <li>{ </ul> <ul> <li>System.out.println(e); </ul> <ul> <li>} </ul>
  24. 24. BufferedReader <ul> <li>class BufferedReader extends Reader </ul> <ul> <li>public BufferedReader(Reader in, int sz) </ul> <ul> <li>public BufferedReader(Reader in) </ul> <ul> <li>public int read() throws IOException </ul> <ul> <li>public int read(char cbuf[], int off, int len) throws IOException </ul> <ul> <li>String readLine(boolean ignoreLF) throws IOException </ul> <ul> <li>public String readLine() throws IOException </ul> <ul> <li>public long skip(long n) throws IOException </ul> <ul> <li>public boolean ready() throws IOException </ul> <ul> <li>public boolean markSupported() </ul> <ul> <li>public void mark(int readAheadLimit) throws IOException </ul> <ul> <li>public void reset() throws IOException </ul> <ul> <li>public void close() throws IOException </ul>
  25. 25. BufferedWriter <ul> <li>class BufferedWriter extends Writer </ul> <ul> <li>public BufferedWriter(Writer out) </ul> <ul> <li>public BufferedWriter(Writer out, int sz) </ul> <ul> <li>public void write(int c) throws IOException </ul> <ul> <li>public void write(char cbuf[], int off, int len) throws IOException </ul> <ul> <li>public void write(String s, int off, int len) throws IOException </ul> <ul> <li>public void newLine() throws IOException </ul> <ul> <li>public void flush() throws IOException </ul> <ul> <li>public void close() throws IOException </ul>
  26. 26. Пример использования BufferedReader/BufferedWriter <ul> <li>try{ // Внешний блок try try{ // Создание потока для записи bw=new BufferedWriter(new FileWriter(&quot;out.txt&quot;)); bw.write(s); }finally{ if(bw!=null) bw.close(); } try{ // Создание потока для чтения br=new BufferedReader(new FileReader(&quot;out.txt&quot;)); String s1=br.readLine(); System.out.println(s1); }finally{ if(br!=null) br.close(); } }catch(Exception e) // Обработка исключений для внешнего try { System.out.println(e); } </ul>
  27. 27. DataInputStream <ul> <li>class DataInputStream extends FilterInputStream implements DataInput </ul> <ul> <li>public DataInputStream(InputStream in) </ul> <ul> <li>void readFully(byte b[]) throws IOException </ul> <ul> <li>void readFully(byte b[], int off, int len) throws IOException </ul> <ul> <li>int skipBytes(int n) throws IOException </ul> <ul> <li>boolean readBoolean() throws IOException </ul> <ul> <li>byte readByte() throws IOException </ul> <ul> <li>int readUnsignedByte() throws IOException </ul> <ul> <li>short readShort() throws IOException </ul> <ul> <li>int readUnsignedShort() throws IOException </ul> <ul> <li>char readChar() throws IOException </ul> <ul> <li>int readInt() throws IOException </ul> <ul> <li>long readLong() throws IOException </ul> <ul> <li>float readFloat() throws IOException </ul> <ul> <li>double readDouble() throws IOException </ul> <ul> <li>String readLine() throws IOException </ul>
  28. 28. DataOutputStream <ul> <li>class DataOutputStream extends FilterOutputStream implements DataOutput </ul> <ul> <li>public DataOutputStream(OutputStream out) </ul> <ul> <li>void write(int b) throws IOException </ul> <ul> <li>void write(byte b[]) throws IOException </ul> <ul> <li>void write(byte b[], int off, int len) throws IOException </ul> <ul> <li>void writeBoolean(boolean v) throws IOException </ul> <ul> <li>void writeByte(int v) throws IOException </ul> <ul> <li>void writeShort(int v) throws IOException </ul> <ul> <li>void writeChar(int v) throws IOException </ul> <ul> <li>void writeInt(int v) throws IOException </ul> <ul> <li>void writeLong(long v) throws IOException </ul> <ul> <li>void writeFloat(float v) throws IOException </ul> <ul> <li>void writeDouble(double v) throws IOException </ul> <ul> <li>void writeBytes(String s) throws IOException </ul> <ul> <li>void writeChars(String s) throws IOException </ul>
  29. 29. Пример использования DataInputStream/DataOutputStream <ul> <li>int i=10; char c='a'; boolean b=true; float f=10.6f; String s=&quot;Hello&quot;; try{ try{ dout=new DataOutputStream(new FileOutputStream(&quot;out2.txt&quot;)); dout.writeInt(i); dout.writeChar(c); dout.writeBoolean(b); dout.writeFloat(f); dout.writeUTF(s); }finally{ if(dout!=null) dout.close(); } </ul>
  30. 30. Пример использования (продолжение) <ul> <li>try{ di=new DataInputStream(new FileInputStream(&quot;out2.txt&quot;)); int j=di.readInt(); char c1=di.readChar(); boolean b1=di.readBoolean(); float f1=di.readFloat(); String s1=di.readUTF(); System.out.println(&quot;Int: &quot;+j+&quot; char: &quot;+c1+&quot; bool: &quot;+b1+&quot; float: &quot;+f1+&quot; Str: &quot;+s1); }finally{ if(di!=null) di.close(); } }catch(Exception e) { System.out.println(e); } </ul>
  31. 31. ObjectInputStream <ul> <li>class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants </ul> <ul> <li>public ObjectInputStream(InputStream in) throws IOException </ul> <ul> <li>public final Object readObject() throws IOException, ClassNotFoundException </ul>
  32. 32. ObjectOutputStream <ul> <li>class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants </ul> <ul> <li>public ObjectOutputStream(OutputStream out) throws IOException </ul> <ul> <li>public final void writeObject(Object obj) throws IOException </ul>
  33. 33. Пример использования ObjectInputStream/ObjectOutputStream <ul> <li>TestClass tc=new TestClass(10,&quot;Hello, world&quot;); System.out.println(tc); try{ try{ oo=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(&quot;out3.txt&quot;))); oo.writeObject(tc); }finally{ if(oo!=null) oo.close(); } try{ oi=new ObjectInputStream(new BufferedInputStream(new FileInputStream(&quot;out3.txt&quot;))); TestClass ttt=(TestClass)oi.readObject(); System.out.println(ttt); }finally{ if(oi!=null) oi.close(); } }catch(Exception e){ System.out.println(e); } </ul>
  34. 34. Конец Вопросы e-mail: a.bovanenko@gmail.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×