08 luồng vào ra trong java

Download 08 Luồng vào ra trong java

Post on 05-Apr-2018

218 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 7/31/2019 08 Lung vo ra trong java

    1/45

    Cc lung vo / ra

  • 7/31/2019 08 Lung vo ra trong java

    2/45

    Cc lung vo ra 2Nguyn Vit H

    Ni dung

    Khi nim v lung d liu Lung v tp

    Lp File

    Truy cp tp tun t Truy cp tp ngu nhin

  • 7/31/2019 08 Lung vo ra trong java

    3/45

    Cc lung vo ra 3Nguyn Vit H

    Ti liu tham kho

    Thinking in Java, chapter 12 Java how to program, chapter 17

  • 7/31/2019 08 Lung vo ra trong java

    4/45

    Cc lung vo ra 4Nguyn Vit H

    Lung d liu (data streams)

    Chng trnh Java nhn v gi d liuthng qua cc i tng l cc thc ththuc mt kiu lung d liu no

    Lung (stream) l mt dng d liu n tmt ngun (source) hoc i n mt ch(sink)

    Ngun v ch c th l tp (file), b nh,mt tin trnh (process), hay thit b (bnphm, mn hnh, )

  • 7/31/2019 08 Lung vo ra trong java

    5/45

    Cc lung vo ra 5Nguyn Vit H

    Lung byte v char

    Lung byte: thao tc theo n v byte InputStream

    OutputStream

    Lung char: thao tc vi k tReader

    Writer

  • 7/31/2019 08 Lung vo ra trong java

    6/45

    Cc lung vo ra 6Nguyn Vit H

    InputStream

    int read()

    int read(byte buf[])

    int read(byte buf[], int

    offset, int length)

    void close()

  • 7/31/2019 08 Lung vo ra trong java

    7/45

    Cc lung vo ra 7Nguyn Vit H

    OutputStream

    int write(int c)

    int write(byte buf[])

    int write(byte buf[], int

    offset, int length)

    void close()

    void flush()

  • 7/31/2019 08 Lung vo ra trong java

    8/45

    Cc lung vo ra 8Nguyn Vit H

    Reader

    int read()

    int read(char buf[])

    int read(char buf[], int

    offset, int length) void close()

  • 7/31/2019 08 Lung vo ra trong java

    9/45

    Cc lung vo ra 9Nguyn Vit H

    Writer

    int write(int c)

    int write(char buf[])

    int write(char buf[], int

    offset, int length) void close()

    void flush()

  • 7/31/2019 08 Lung vo ra trong java

    10/45

  • 7/31/2019 08 Lung vo ra trong java

    11/45

    Cc lung vo ra 11Nguyn Vit H

    Ph h ca OutputStream

  • 7/31/2019 08 Lung vo ra trong java

    12/45

    Cc lung vo ra 12Nguyn Vit H

    Ph h ca Reader

  • 7/31/2019 08 Lung vo ra trong java

    13/45

    Cc lung vo ra 13Nguyn Vit H

    Ph h ca Writer

  • 7/31/2019 08 Lung vo ra trong java

    14/45

    Cc lung vo ra 14Nguyn Vit H

    i tng vo / ra

    nhp hoc xut d liu, chng ta phito ra i tng vo hoc ra

    i tng vo hoc ra thuc kiu lung

    tng ng v phi c gn vi mtngun d liu hoc mt ch tiu th dliu

  • 7/31/2019 08 Lung vo ra trong java

    15/45

    Cc lung vo ra 15Nguyn Vit H

    S dng b m

    B m l mt k thut tng tnh hiu qu ca thao

    tc vo / ra c v ghi d liu theo khi gim s ln thao tc vi thit b

    Thay v ghi trc tip ti thit b th chng trnh ghi ln

    bm khi bm y th d liu c ghi ra thit b theo khi c th ghi vo thi im bt k bng phng thc flush()

    Thay v c trc tip t thit b th chng trnh c tbm khi bm rng th d liu c c theo khi t thit b

  • 7/31/2019 08 Lung vo ra trong java

    16/45

    Cc lung vo ra 16Nguyn Vit H

    Nhp xut qua thit b chunConsole I/O

    System.out cho php in ra lung ra chun l i tng ca lp PrintStream

    System.err cho php in ra lung thng bo

    li chun l i tng ca lp PrintStream

    System.in cho php c vo t thit b vochun l i tng ca lp InputStream

  • 7/31/2019 08 Lung vo ra trong java

    17/45

  • 7/31/2019 08 Lung vo ra trong java

    18/45

    Cc lung vo ra 18Nguyn Vit H

    V d:InputStreamReader reader = new InputStreamReader(System.in);

    BufferedReader in = new BufferedReader(reader);---

    String s;

    try {s = in.readLine();

    }

    catch (IOException e) {...}

  • 7/31/2019 08 Lung vo ra trong java

    19/45

    Cc lung vo ra 19Nguyn Vit H

    Lp File

    Mt trong cc ngun v ch d liu thngthng l tp

    Lp File cung cp cc chc nng c bn

    thao tc vi tpnm trong gi java.io

    to tp, m tp, cc thng tin v tp v th

    mc

  • 7/31/2019 08 Lung vo ra trong java

    20/45

    Cc lung vo ra 20Nguyn Vit H

    To i tng File

    File myFile;

    myFile = new File(data.txt);

    myFile = new File(myDocs, data.txt);

    Th mc cng c coi nh l mt tpFile myDir = new File(myDocs);

    File myFile = new File(myDir, data.txt);

    c phng thc ring thao tc vi th mc

  • 7/31/2019 08 Lung vo ra trong java

    21/45

    Cc lung vo ra 21Nguyn Vit H

    Cc phng thc Tn tp

    String getName()

    String getPath()

    String getAbsolutePath()

    String getParent()

    boolean renameTo(File newName)

    Kim tra tpboolean exists()

    boolean canWrite()

    boolean canRead()

    boolean isFile()boolean isDirectory()

    boolean isAbsolute()

  • 7/31/2019 08 Lung vo ra trong java

    22/45

  • 7/31/2019 08 Lung vo ra trong java

    23/45

    Cc lung vo ra 23Nguyn Vit H

    Thao tc vi tp k t

    c t tpFileReader: c k t t tp

    BufferedReader: c c bm (c tng

    dng readLine()) Ghi ra tpFileWriter: ghi k t ra tp

    PrintWriter: ghi theo dng (print() v println())

  • 7/31/2019 08 Lung vo ra trong java

    24/45

    Cc lung vo ra 24Nguyn Vit H

    V d:c vo t tpFile file = new File(data.txt);

    FileReader reader = new FileReader(file);BufferedReader in = new BufferedReader(reader);

    String s;

    try {

    s = in.readLine();}

    catch (IOException e) {

    }

  • 7/31/2019 08 Lung vo ra trong java

    25/45

    Cc lung vo ra 25Nguyn Vit H

    V d:c vo (cont.)File file = new File(data.txt);

    FileReader reader = new FileReader(file);

    BufferedReader in = new BufferedReader(reader);Abc abc = new Abc();

    abc.read(in);

    abc.doSomething();

  • 7/31/2019 08 Lung vo ra trong java

    26/45

    Cc lung vo ra 26Nguyn Vit H

    V d:c vo (cont.)class Abc {

    public void read(BufferedReader in) {

    String s;try {

    s = in.readLine();

    ...

    }catch (IOException e) {}

    }

    public void doSomething() {...}

    ...}

  • 7/31/2019 08 Lung vo ra trong java

    27/45

  • 7/31/2019 08 Lung vo ra trong java

    28/45

    Cc lung vo ra 28Nguyn Vit H

    V d: Ghi ra (cont.)class Abc {

    ...public void write(PrintStream out) {

    ...

    try {

    out.println(s);

    out.close();

    }

    catch (IOException e) {...}

    }

    }

  • 7/31/2019 08 Lung vo ra trong java

    29/45

    Cc lung vo ra 29Nguyn Vit H

    V d: Ghi ra (cont.)class Abc {

    ...public String write() {

    String buf;

    buf += ...

    return buf;

    }

    }

  • 7/31/2019 08 Lung vo ra trong java

    30/45

    Cc lung vo ra 30Nguyn Vit H

    V d: File copyimport java.io.*;

    public class CopyFile {

    public static void main(String args[]) {

    try {

    FileReader src = new FileReader(args[0]);

    BufferedReader in = new BufferedReader(src);

    FileWriter des = new FileWriter(args[1]);

    PrintWriter out = new PrintWriter(des);

    String s;

    s = in.readLine();

    while (s != null) {out.println(s);

    s = in.readLine();

    }

    in.close();

    out.close();

    }catch (IOException e) {

    e.printStackTrace();

    }

    }

    }

  • 7/31/2019 08 Lung vo ra trong java

    31/45

  • 7/31/2019 08 Lung vo ra trong java

    32/45

    Cc lung vo ra 32Nguyn Vit H

    Thao tc vi tp d liu (tun t)

    c d liu FileInputStream: c d liu t tp

    DataInputStream: c d liu kiu nguyn thy

    ObjectInputStream: c i tng

    Ghi d liu FileOutputStream: ghi d liu ra tp

    DataOutputStream: ghi d liu kiu nguyn thy

    ObjectOutputStream: ghi i tng

  • 7/31/2019 08 Lung vo ra trong java

    33/45

  • 7/31/2019 08 Lung vo ra trong java

    34/45

    Cc lung vo ra 34Nguyn Vit H

    Ghi d liu nguyn thy (tun t)import java.io.*;

    public class TestDataOutputStream {

    public static void main(String args[]) {int a[] = {2, 3, 5, 7, 11};

    try {

    FileOutputStream fout = new FileOutputStream(args[0]);

    DataOutputStream dout = new DataOutputStream(fout);

    for (int i=0; i

  • 7/31/2019 08 Lung vo ra trong java

    35/45

    Cc lung vo ra 35Nguyn Vit H

    c d liu nguyn thy (tun t)import java.io.*;

    public class TestDataInputStream {

    public static void main(String args[]) {

    try {

    FileInputStream fin = new FileInputStream(args[0]);

    DataInputStream din = new DataInputStream(fin);

    while (true) {System.out.println(din.readInt());

    }

    }

    catch (EOFException e) {

    }

    catch (IOException e) {e.printStackTrace();

    }

    }

    }

  • 7/31/2019 08 Lung vo ra trong java

    36/45

    Cc lung vo ra 36Nguyn Vit H

    i tng tun t

    Mt i tng c th c lu trong bnh ti nhiu vng nh khc nhaucc thuc tnh khng phi l kiu nguyn thy

    i tng mun ghi / c c phi thuclp c ci t giao din Serializabley l giao din nhn, khng c phng thc

  • 7/31/2019 08 Lung vo ra trong java

    37/45

    Cc lung vo ra 37Nguyn Vit H

    import java.io.Serializable;

    class Recordimplements Serializable {

    private String name;

    private float score;

    public Record(String s, float sc) {

    name = s;

    score = sc;}

    public String toString() {

    return "Name: " + name + ", score: " + score;

    }}

  • 7/31/2019 08 Lung vo ra trong java

    38/45

  • 7/31/2019 08 Lung vo ra trong java

    39/45

    Cc lung vo ra 39Nguyn Vit H

    import java.io.*;

    public class TestObjectInputStream {public static void main(String args[]) {

    Record r;

    try {

    FileInputStream fin = new FileInputStream(args[0]);

    ObjectInputStream in = new ObjectInputStream(fin);

    while (true) {

    r = (Record) in.readObject();

    System.out.println(r);

    }

    }

    catch (EOFException e) {

    System.out.println("No more records");

    }

    catch (ClassNotFoundException e) {

    System.out.println("Unable to create object");

    }

    catch (IOException e) {e.printStackTrace();

    }

    }

    }

  • 7/31/2019 08 Lung vo ra trong java

    40/45

    Cc lung vo ra 40Nguyn Vit H

    Lp RandomAccessFile

    L mt lp c lp (k tha trc tip tObject)

    m nhn vic c v ghi d liu ngu

    nhinci t cc giao din DataInput v DataOutput

    Kch thc bn ghi phi cnh

  • 7/31/2019 08 Lung vo ra trong java

    41/45

    Cc lung vo ra 41Nguyn Vit H

    import java.io.*;

    public class WriteRandomFile {

    public static void main(String args[]) {

    int a[] = { 2, 3, 5, 7, 11, 13 };

    try {

    File fout = new File(args[0]);RandomAccessFile out;

    out = new RandomAccessFile(fout, "rw");

    for (int i=0; i

  • 7/31/2019 08 Lung vo ra trong java

    42/45

    Cc lung vo ra 42Nguyn Vit H

    import java.io.*;

    public class ReadRandomFile {

    public static void main(String args[]) {

    try {

    File fin = new File(args[0]);RandomAccessFile in = new RandomAccessFile(fin, "r");

    int recordNum = (int) (in.length() / 4);

    for (int i=recordNum-1; i>=0; i--) {

    in.seek(i*4);

    System.out.println(in.readInt());}

    }

    catch (IOException e) {

    e.printStackTrace();

    }}

    }

  • 7/31/2019 08 Lung vo ra trong java

    43/45

    Cc lung vo ra 43Nguyn Vit H

    Lp Scanner

    L lp mi h tr nhp d liu, k thatrc tip t Object (t Java 1.5)

    Khi to vi i s l i tng vo

    (lung, tp, xu k t) C cc phng thc h tr nhp trc tipnextType, hasNextType

  • 7/31/2019 08 Lung vo ra trong java

    44/45

    Cc lung vo ra 44Nguyn Vit H

    Scanner sc = new Scanner(System.in);int i = sc.nextInt();

    ---

    Scanner sc;

    sc = new Scanner(new File("myNumbers"));

    while (sc.hasNextLong()) {

    long aLong = sc.nextLong();

    }

    --

    String str = 2 3 5 7;

    Scanner sc = new Scanner(str);

    while (sc.hasNextInt()) {

    System.out.println(sc.nextInt());

    }

  • 7/31/2019 08 Lung vo ra trong java

    45/45

    Cc lung vo ra 45Nguyn Vit H

    class Abc {

    public void read(Scanner sc) {

    ...}

    public void doSomething() {...}

    ...

    }