Top Banner

of 12

Java Programming - Chapter 6 (in Thai)

Feb 26, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    1/12

    65

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    CHAPTER

    6

    (File Operations)

    1. (Characteristic of File)

    1. 1)

    (File / Text File) * . txt , *. dat

    2)

    ()3) () 4)

    (1 = 1 Byte)

    2.

    [1] [2] 1) (Reading File)

    2)

    (Writing File)

    2. (Reading and Writing Files)

    1.

    Scanner 2 -1)

    Scanner java.io Scanner java.io import java.util.Scanner;import java.io.*;

    "dat a. t xt " Notepad

    * . txt

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    2/12

    66

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    1

    2

    345

    123

    4567

    12345

    6

    78

    2) ( mai n) throws IOException

    3)

    4)

    1-3 hasNext()(Boolean)

    import java.util.Scanner;import java.io.*;

    publ i c cl ass Test {. . .

    }

    i mpor t 2

    i mpor t j ava. ut i l . Scanner ;i mpor t j ava. i o. * ;publ i c cl ass Test {

    publ i c stati c voi d mai n( St r i ng[ ] ar gs) throws IOException {. . .}

    }

    Scanner = new Scanner ( new Fi l e( "") ) ;

    i mpor t j ava. ut i l . Scanner ;i mpor t j ava. i o. * ;publ i c cl ass Test {

    publ i c st at i c voi d mai n( St r i ng[ ] ar gs) t hr ows I OExcept i on {Scanner in = new Scanner(new File("data.txt"));

    . . .

    }}

    Fi l e = new Fi l e( "")Scanner = new Scanner ( ) ;

    Syst em. i n i n

    dat a. t xt

    whi l e( . hasNext()) {. . .}

    (Next )

    t hrowss

    ()

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    3/12

    67

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    12

    34

    5

    67

    891011

    12

    1314

    5) -

    (1)i nt next I nt ( )

    i nt num = i n. next I nt ( ) ; (2)doubl enext Doubl e( )

    doubl e d = i n. next Doubl e( ) ; (3)Str i ngnext Li ne( )

    St r i ng s1 = i n. nextLi ne( ) ; (4)St r i ng(\ t \ n)

    next( )

    St r i ng s2 = i n. next( ) ;

    6)

    close()

    7)

    i nt = .nextInt();

    doubl e = .nextDouble();

    St r i ng = .nextLine();

    St r i ng = .next();

    next I nt ( )

    .close();

    import java.util.Scanner;import java.io.*;

    publ i c cl ass Test {publ i c stati c voi d mai n( St r i ng[ ] ar gs) throws IOException {Scanner in = new Scanner(new File("d:/cu/student.dat"));

    i nt i = 1;while(in.hasNext()) {

    St r i ng s = i n. next Li ne( ) ;Syst em. out . pr i nt l n( i + " : " + s) ;i ++;

    }in.close();

    }}

    st udent . dat

    i n. cl ose( ) ;

    Full Path

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    4/12

    68

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    1 [] (12 )

    2 [] data.txt (Number of Lines) (10 )

    test.txt1234

    5

    Scanner i n = new Scanner ( new Fi l e( " t est . t xt " ) ) ;whi l e (i n. hasNext ( ) ) {System.out.println( );

    }

    i n. cl ose( ) ;

    in.nextInt() in.nextDouble() in.nextLine() in.next()

    while while while while

    1 2 345 6

    7. 0 8 9D 0

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    5/12

    69

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    3 [] data.txt (Number of Words) (10 )

    4 [

    ]

    data.txt

    (Number of Characters) (10 )

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    6/12

    70

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    5 [] student.txt 2110191 Innovative Thinking

    (10 )

    21import java.util.Scanner;

    import java.io.*;public class CountEngStudent {

    public static void main(String[] args) throws IOException {

    } //End of main

    } //End of class

    6 [] score.txt(100 )

    60 S U (10 )

    import java.util.Scanner;import java.io.*;

    public class StudentGrade {

    public static void main(String[] args) throws IOException {

    score.txt

    1. 5330000221 U2. 5330000320 S3. 5330000421 S4. 5330000521 U5. 5330000622 S. . .

    5330000221 45. 23 215330000320 72. 45 205330000421 91. 11 215330000521 38. 20 215330000622 60. 00 22. . .

    student.txt

    Engi neer i ng St udent s: 2

    1. Wongyos Kear dsr i 52718228212. Wannapor n Yi m- ngam 53300234223. Ong- ar d Rut samee 5230586021. . .

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    7/12

    71

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    } //End of main} //End of class

    7 [ ] student.dat

    (15 )1) 5330000121 1st2)

    1 Preecha P. wongyos W.

    3)

    2.00 Pass 1.002.00Critical 1.00Retired4)

    import java.util.Scanner;

    import java.io.*;public class StudentInfoFromFile {public static void main(String[] args) throws IOException {

    1. 4830000121 6t h P. Ler dsi r i kul 1. 99 Cr i t i cal2. 5230000221 2nd S. Chokkhoukhang 2. 02 Pass3. 5330000321 1st R. Suksawat dee 0. 56 Ret i r ed. . .

    student.dat

    4830000121preechaLerds i r i kul1. 995230000221Suwar apornChokkhoukhang2. 025330000321

    Ruksaj aiSuksawat dee0. 56. . .

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    8/12

    72

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    } //End of main} //End of class

    8 [ ] love song.txt 2 () love you my lovely love 1 () love you my lovely love 2 (15 )

    Count Wor ds #1: 3Count Wor ds #2: 7

    I Love you al l over LOvE j ava.I l OVe some l ovel y f r i end.Lovel oVe j ava! ! .

    song.txt

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    9/12

    73

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    9 [] data1.dat data2.dat 2110101 2552 2553 F

    F (15 )

    import java.util.Scanner;

    import java.io.*;public class FComparison {public static void main(String[] args) throws IOException {

    No. Student I D Gr ade========================1. 5230000121 C+2. 5230000221 A3. 5230000321 B+4. 5230000421 F5. 5230000521 B. . .

    data1.dat

    No. Student I D Gr ade========================1. 5330000121 F2. 5330000221 F3. 5330000321 F4. 5330000421 D+5. 5330000521 A. . .

    data2.dat

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    10/12

    74

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    } //End of main} //End of class

    2.

    PrintStream 1) PrintStream PrintStream

    java.io import java.io.*;

    2)

    throws IOException

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    11/12

    75

    2553 (7 2/2553) ()

    Computer Programming using JavaFile Operations - 06

    12345

    678

    12

    34

    5

    67

    89

    1011

    3)

    4)

    1)

    out . pr i nt ( "St ar t New Fi l e") ; 2)

    out . pr i nt l n( "St ar t New Fi l e") ;

    5) close()

    6)

    10 [] -

    sentence.txt Stop (10 )

    .println( ) ;

    .print( ) ;

    Pr i nt St r eam = new Pr i ntSt r eam( new Fi l e( "") ) ;

    i mpor t j ava. ut i l . Scanner ;i mpor t j ava. i o. * ;publ i c cl ass Test {

    publ i c st at i c voi d mai n( St r i ng[ ] ar gs) t hr ows I OExcept i on {PrintStream out = new PrintStream(new File("data.txt"));

    . . .}

    } out dat a. t xt

    i mpor t j ava. ut i l . Scanner ;import java.io.*;

    publ i c cl ass Test {publ i c stati c voi d mai n( St r i ng[ ] ar gs) throws IOException {PrintStream out = new PrintStream(new File("data.dat"));

    f or ( i nt i = 1; i

  • 7/25/2019 Java Programming - Chapter 6 (in Thai)

    12/12

    76

    2553 (7 2/2553) ()

    Computer Programming using Java 06 - File Operations

    11 [] text.txt We love java avaj evol eW revtext.txt(15 )

    text.txt

    We l ove j ava

    J ava i s my f i r st programmi ng andJ ava i s my l ast programmi ng. . .

    revtext.txt

    avaj evol eW

    dna gni mmar gorp t sr i f ym si avaJgni mmar gorp t sal ym si avaJ. . .