I/O ????? - PowerPoint PPT Presentation

About This Presentation
Title:

I/O ?????

Description:

Title: AI Author: Last modified by: hey Created Date: 11/3/2000 6:26:37 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 16
Provided by: 66459
Category:

less

Transcript and Presenter's Notes

Title: I/O ?????


1
I/O ?????
  • ??? ????(I)
  • ???? ???

2
stream ????
  • stream
  • ???? ??(source) ?? ??(destination)? ?? ?? ?? ???
    ???? ??
  • ????? ????? ?? ?????? ???? ??? ?(sequence of
    bytes)??.
  • ????? stream? ???? ??? ??.
  • Stream programming? ??? ???? ???? ?(type)? ?? ??
    ???? ??? ?? ????? ????.
  • input stream? output stream
  • input stream ??? ???? ??? ?? ?? ??? ?? data?
  • ?? ? ??
  • output stream ??? ?? ??? ? ?? ??(??, ?? ???)?
  • data? ? ? ??

3
FileInputStream FileOutputStream (1)
  • FileInputStream
  • ??? ??? ?? ??? FileInputStream ??? ??
  • FileInputStream ???? ??? ???? ???
    FileNotFoundException? ?? ???.
  • FileInputStream myStream new
    FileInputStream(C\data\aaa.txt)
  • FileOutputStream
  • ??? ??? ?? ??? FileOutputStream ??? ??
  • ??? ?? ??? ???? ??? SecurityException? ?????.
  • FileOutputStream myStream new
    FileOutputStream(bbb.txt)

4
FileInputStream FileOutputStream (2)
  • import java.io. // command line
    argument? ???? ?? ?? ??

  • // ??? ??? copy
  • public class FileCopy
  • public static void main(String args) throws
    Exception
  • if (args.length lt 2)
  • System.out.println("Usage java CopyFile
    file1 file2")
  • return
  • FileInputStream fis new
    FileInputStream(args0) //???? ? ?? ?? ??
  • FileOutputStream fos new
    FileOutputStream(args1) //???? ? ?? ??? ??
  • int readByte 0
  • while ((readByte fis.read()) ! -1)
    // EOF?? 1byte? ???
  • fos.write(readByte) // 1 byte?
    ??
  • fis.close()
  • fos.close()
  • System.out.println(args0 " copied to
    " args1)

5
DataInputStream DataOutputStream (1)
  • FileInputStream, FileOutputStream - ?? byte???
    I/O? ??
  • DataInputStream, DataOutputStream - ?? primitive
    I/O? ???

  • ??? ??
  • Java? ??? ???? ?? ? 16bit-UniCode??? ??
  • ??? byte ??? I/O?? ?? ?? ??? ?? ??? ??.(?? gt
    2byte)
  • I/O? ? ??? 16bit-Unicode? ???? I/O class?? ????.
  • Reader, Writer ????? ?? ?? ???? 2byte ????
  • ???? stream

6
DataInputStream DataOutputStream (2)
  • DataOutputStream Class Method summary

Method summary Method summary
writeByte(int value) Int ??? ?? ???? ???? ??.
writeBoolean(boolean value) Boolean?? true ?? 1?, false?? 0? ???? ? ??? ??? ??.
writeChar(int value) ??? ?? ? ???? ???? ??.
writeShort(int value) ??? ?? ? ???? ???? ??.
writeInt(int value) int ??? ? ??? ??? ???? ??.
writeLong(long value) Long ??? ?? ??? ??? ???? ??.
writeFloat(float value) Float ??? ? ???? ???? ??.
writeDouble(double value) Double ??? ?? ???? ???? ??.
7
DataInputStream DataOutputStream (3)
  1. import java.io. //???
    ??? ???? ??? ??? ?? ??? ??
  2. public class ExDataStream
  3. public static void main(String args)
    throws Exception
  4. if (args.length lt 1 )
  5. System.out.println("???? java
    ExDataStream.java aa.txt")
  6. return
  7. FileOutputStream fos new
    FileOutputStream(args0)
  8. DataOutputStream dos new
    DataOutputStream(fos) //privitive type size?
    i/o? ??
  9. dos.writeUTF("???") // UTF-8 encoding
    ?? ??? ??
  10. dos.writeUTF("41456") // UTF-8?
    16bit ????? 8bit ??? ??
  11. dos.writeInt(30)
  12. dos.close()
  13. FileInputStream fis new
    FileInputStream(args0)
  14. DataInputStream dis new
    DataInputStream(fis)
  15. System.out.println("?? "
    dis.readUTF()) //??? ??? ??? ??
  16. System.out.println("?? "
    dis.readUTF())
  17. System.out.println("?? "
    dis.readInt())
  18. dis.close()

8
reader writer
  1. import java.io. //?????? ???? ?? ?? ???
    ??
  2. public class StringInput
  3. public static void main(String args)
    throws Exception
  4. String inputString
  5. InputStreamReader isr new
    InputStreamReader(System.in)
  6. BufferedReader br new BufferedReader(isr)
  7. FileOutputStream fos new
    FileOutputStream(args0)
  8. OutputStreamWriter osr new
    OutputStreamWriter(fos)
  9. BufferedWriter bw new BufferedWriter(osr)
  10. inputString br.readLine()
    //readLine()? ??? ?? ??? ??? ??? ?? ? ?? ??.
  11. bw.write(inputString "\n") //??? ??? ??
  12. bw.close() //??? ?? stream object?
    close? ?? ?? ?? stream object? close?? ??.
  13. br.close()

9
File Class
  • File Class
  • File Object? ???? ??? ?? ??? ?? ??? ???? ??????
    ???? ????.
  • File Object? ?? ?? ??? ????? ??? ? ??.
  • ???? ?????? ???? ???? ??? ?? ? ?? ???, ?? ??? ???
    ? ? ?? ?? ?? ???? ????.

Method summary Method summary
exists() File ??? ???? ???? ????? ???? true, ??? false ??
isDirectory() File ??? ????? ???? true, ??? false ??
isFile() File ??? ??? ???? true, ??? false ??
canRead() File ??? ???? ??? ?? ? ??? true, ??? false ????. ??? ?? ?? SecurityException? ???? ? ??.
canWrite() File ??? ???? ??? ? ? ??? true, ??? false ?? ??. ??? ? ? ??? SecurityException? ???? ? ??.
10
URL Class
  • URL Class
  • Java.net ???? ?? ?? ??.
  • URL??? ?? URL? ?? ??? ??? ???, ???? ??? ??? ???
    ????? ?? ? ??.
  • URL? ???? ??? ???? ??? ??? ???? ? ? ??.
  • ??? ?? URL? local pc? ???? ?? ? ??.
  • import java.io. // ???
    ?? ??? ???? ???? ??
  • import java.net. //URL
    Class?????? ??? import??.
  • public class GetIndexHtml
  • public static void main(String args)
    throws Exception
  • byte inputString new byte1024
  • InputStream is (new URL(args0)).openS
    tream()
  • FileOutputStream fos new
    FileOutputStream(args1)
  • while(is.read(inputString, 0,
    inputString.length) ! -1)
  • fos.write(inputString)
  • fos.close()

11
RandomAccessFile(1)
  • RandomAccessFile
  • ??? ??? ???? ? ??
  • ???, ?? ????? ??? ??? ??? ? ?? ??(RandomAccess)?
    ??
  • RandomAccessFile raf new
    RandomAccessFile("c\myfile.txt", "r/w")

Method summary Method summary
seek(long pos) ??? ?? ??? pos ??? ??? ??? ???
getFilePointer() ??? ?? ??(??? ??????? ??)? long ???? ????.
length() ??? ??? ??? ??? long ????? ????.
12
RandomAccessFile(2)
  • import java.io.
  • import java.util.
  • public class RandomWrite
  • public static void main(String args)
    throws Exception
  • RandomAccessFile raf new
    RandomAccessFile("Mylog.log", "rw")
  • raf.seek(raf.length()) //??? ?? ?????
    ?? ??? ???.
  • raf.writeUTF(new Date().toString())
    //?? ??? ??.
  • raf.close()

13
Serialization(1)
  • Serialization
  • ??? ?? ??? ???? ?? ?? ??
  • ???? ???? ?? ??? type ???? ?? ???? ???? ?? ?? ?
    ?? ??? ??
  • serializable? ? ? ?? object? class?
    Serializable??? ?? interface? ??? implements??
    ??.
  • Serializable interface
  • serializable interface implements? method? ??.
  • Serializable? implements??? ?? ?
    class?serializable? ????? ????.
  • Object? serialization? ? data??(member??)? ????
    constructor? method? code? ???? ???.

14
Serialization(2)
  1. import  java.io. //primitive
    type? ?? object? ??? ?? ?? ??
  2.  public class TestSerialization implements
    Serializable
  3.          String name
  4.           int age 7
  5.          TestSerialization (String name, int age)
  6.                       this.name name
  7.                       this.age age
  8.       
  • public class SerializeMyObject
    //TestSerialization class? object? ??? write??
  •             TestSerialization ts
  •               public static void main(String
    args) throws Exception
  •                           SerializeMyObject smo
    new SerializeMyObject()
  •                           smo.writeMyObject()
    // object? file? write?.
  •                           smo.ts null
    // object? memory?? ??.
  •                           smo.readMyObject()
    // ???? object? ?? ???.
  •                           System.out.println(smo.t
    s.name)
  •               

15
Serialization(3)
  •        public void writeMyObject() throws
    Exception //object? ??? write
  •                  ts new TestSerialization("???",
    32)
  •                  ObjectOutputStream oos
  •                   new ObjectOutputStream(new
    FileOutputStream("myObject.txt"))
  •                   oos.writeObject(ts) // object?
    ??? write?.
  •                   oos.close()
  •         
  •  
  •         public void readMyObject() throws
    Exception // ??? ??? object? ??


  • //???? ??? ???
  •                  ObjectInputStream ois
  •                  new ObjectInputStream(new
    FileInputStream("myObject.txt"))
  •                  ts (TestSerialization)ois.readO
    bject() // object? ???? ??? memory? ??.
  •                  ois.close()
  •         
Write a Comment
User Comments (0)
About PowerShow.com