งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

File I/O (1) โปรแกรมจะอ่านหรือเขียนข้อมูลผ่านท่อส่งข้อมูล (Stream)

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "File I/O (1) โปรแกรมจะอ่านหรือเขียนข้อมูลผ่านท่อส่งข้อมูล (Stream)"— ใบสำเนางานนำเสนอ:

1 File I/O (1) โปรแกรมจะอ่านหรือเขียนข้อมูลผ่านท่อส่งข้อมูล (Stream)
จากต้นทาง (source) ไปยังปลายทาง (sink) สามารถรับส่งข้อมูลแบบ Byte Stream และ Character Stream

2 File I/O (2) การรับส่งข้อมูลแบบ Byte Stream => อ่านและเขียนไฟล์
ครั้งละ 1 ไบต์ (8 บิต) ใช้สำหรับข้อมูลประเภทตัวเลข การรับส่งข้อมูลแบบ Character Stream => อ่านและเขียนไฟล์ ครั้งละ 2 ไบต์ (16 บิต) ใช้สำหรับข้อมูลประเภทตัวอักขระ คลาส Byte Stream => FileInputStream, FileOutputStream คลาส Character Stream => FileReader, FileWriter

3 File I/O (3) Buffer Stream เป็นที่พักในหน่วยความจำสำหรับการรับส่งข้อมูลผ่าน buffer ช่วยให้โปรแกรมทำงานเร็วขึ้น คลาส Byte Stream => BufferedInputStream, BufferedOutputStream คลาส Character Stream => BufferedReader, BufferedWriter

4 File I/O (4) เนื่องจากข้อมูลที่เขียนเป็นทั้งประเภทตัวอักขระและตัวเลข
จึงเลือกใช้การเขียนและอ่านไฟล์แบบ Character Stream การอ่านไฟล์จึงใช้คลาส FileReader คู่กับ BufferedReader (P.246) File filename = "c:\\CharBuffData.txt"; FileReader f_read = null; BufferedReader b_read = null; try { f_read = new FileReader(filename); b_read = new BufferedReader(f_read); String c; while ((c = b_read.readLine()) != null) System.out.print(c); } catch (IOException e) { System.out.println(e);

5 File I/O (5) ส่วนการเขียนไฟล์จะใช้คลาส FileWriter คู่กับ BufferedWriter และใช้ร่วมกับคลาส PrintWriter มีเมธอด print(), println() (P.251) File filename = "c:\\WriteBuffData.txt"; FileWriter f_write = null; BufferedWriter b_write = null; PrintWriter p_write = null; int id=1; String name = “JAVA”; float rating = 4.5f; try { f_write = new FileWriter(filename); b_write = new BufferedWriter(f_write); p_write = new PrintWriter(b_write); pwrite.println(id+name+rating); } catch (IOException e) { System.out.println(e);

6 ตัวอย่างโปรแกรม 14.6 (P.251-252) import java.io.*;
import java.util.Scanner; public class CharWriteTest { public static void main(String[] args) { int id; String name, score; File ofile = new File("c:\\CharScore.txt"); FileWriter f_write = null; BufferedWriter b_write = null; PrintWriter p_write = null;

7 ตัวอย่างโปรแกรม 14.6 (P.251-252) try {
f_write = new FileWriter(ofile); b_write = new BufferedWriter(f_write); p_write = new PrintWriter(b_write); System.out.println("Read from KB"); System.out.println("==========================="); do { System.out.print("id = "); id = new Scanner(System.in).nextInt(); if (id != -1) { System.out.print("name = "); name = new Scanner(System.in).nextLine(); System.out.print("score = "); score = new Scanner(System.in).nextLine(); p_write.println(id+"\t"+name+"\t"+score); } } while (id != -1);

8 ตัวอย่างโปรแกรม 14.6 (P.251-252) catch (IOException e) {
System.out.println(e); } finally { if (p_write != null) { p_write.close(); } System.out.println("Data are saved to File : \n" + ofile); System.out.println("==========================="); } // main } // class

9 ตัวอย่างโปรแกรม 14.7 (P.253-254) import java.io.*; class std { int id;
String name; double score; } public class CharReadTest { public static void main(String[] args) { File ifile = new File("c:\\CharScore.txt"); FileReader f_read = null; BufferedReader b_read = null; std[] Sd = new std[20]; double sum=0; int c=0; String line;

10 ตัวอย่างโปรแกรม 14.7 (P.253-254) try { f_read = new FileReader(ifile);
b_read = new BufferedReader(f_read); System.out.println("Read Data from File : \n" + ifile); System.out.println("==========================="); while ((line = b_read.readLine()) != null) { Sd[c] = new std(); String msg[] = line.split("\t",3); Sd[c].id = Integer.parseInt(msg[0]); Sd[c].name = msg[1]; Sd[c].score = Double.parseDouble(msg[2]); sum = sum+x[c].s; c++; }

11 ตัวอย่างโปรแกรม 14.7 (P.253-254) catch (IOException e) {
System.out.println(e); } finally { try { if (b_read != null) b_read.close(); double avg =sum/c; for (int i=0;i<c;i++) { System.out.printf("%-3d%-10s%-6.2f\n",x[i].i,x[i].n,x[i].s); } System.out.println("==========================="); System.out.printf("mean = %.2f\n",avg); } catch (IOException e) { } //finally } //main } //class


ดาวน์โหลด ppt File I/O (1) โปรแกรมจะอ่านหรือเขียนข้อมูลผ่านท่อส่งข้อมูล (Stream)

งานนำเสนอที่คล้ายกัน


Ads by Google