1. 程式人生 > >javaIO--字節流

javaIO--字節流

rom 序列值 ret system acc 輸入 out 個數 --

流---是指的一組有序的、有氣墊和重點的字節集合,是對的護具傳輸的總稱或者抽象。

流采用緩沖區技術,當寫一個數據時,系統將數據發送到緩沖區而不是外部設備(如硬盤),當讀一個數據時,系統實際是從緩沖區讀取數據的。

流的存在:我們都知道輸入流和輸出流,二者的定義都是基於數據流向且是以內存為坐標的。標準輸入過程中,數據從鍵盤等輸入設備流向內存,這事輸入流。標準輸出過程中,數據從內存輸出到顯示器或者打印機等設備,這是輸出流。

流分為字節流和字符流

下邊主要講標準輸入輸出、文件字節流、數據字節流和對象字節流四種字節流。

  1 public class IOtest {
  2     
  3     public
static void main(String[] args) throws IOException { 4 5 // KeyboardInput(); 6 7 // byte[] buffer = {0,1,2,3,4,5,6,7,8,9}; 8 // ByteFile afile = new ByteFile("info.txt"); 9 // afile.writeToFile(buffer); 10 // afile.readFromFile(); 11 // afile.copyFile("io2.txt");
12 13 // IntFile afile = new IntFile("fileInt.txt"); 14 // afile.writeToFile(); 15 // afile.readFromFile(); 16 17 Student stus[] = {new Student("張三", "男"), new Student("李四", "女"), new Student("王五", "女博士")}; 18 objectFile afile = new objectFile("students.dat");
19 afile.writeToFile(stus); 20 System.out.println("stus: \n" + afile.readFromFile()); 21 } 22 23 //標準輸入輸出 24 public static void KeyboardInput() throws IOException{ 25 System.out.print("Input:"); 26 byte buffer[] = new byte[512]; //以字節數組作為緩沖區 27 int count = System.in.read(buffer); 28 System.out.print("Output:"); 29 for(int i=0; i<count; i++){ 30 System.out.print(" " + buffer[i]); 31 } 32 System.out.println("\ncount = " + count); 33 for(int i=0; i<count; i++){ 34 System.out.print((char)buffer[i]); 35 } 36 } 37 38 //文件字節流 39 static class ByteFile{ 40 public String filename; 41 public ByteFile(String filename){ 42 this.filename = filename; 43 } 44 //字節流寫 45 public void writeToFile(byte[] buffer) throws IOException{ 46 FileOutputStream fout = new FileOutputStream(this.filename); 47 fout.write(buffer); 48 fout.close(); 49 } 50 //字節流讀 51 public void readFromFile() throws IOException{ 52 FileInputStream fin = new FileInputStream(this.filename); 53 System.out.print(this.filename + ":"); 54 byte[] buffer = new byte[512]; 55 int count=0; 56 while(count != -1){ 57 count = fin.read(buffer); //read返回值為字節數目,當為空時 返回-1 58 for(int i=0; i<count; i++){ 59 System.out.print(buffer[i] + " "); 60 } 61 System.out.println(" count = " + count); 62 } 63 fin.close(); 64 } 65 //文件字節流復制到filename2 66 public void copyFile(String filename2) throws IOException{ 67 FileInputStream fin = new FileInputStream(this.filename); 68 FileOutputStream fout = new FileOutputStream(filename2); 69 byte[] buffer = new byte[512]; 70 int count = fin.read(buffer); //讀取輸入流並返回流的大小 71 while(count != -1){ 72 fout.write(buffer, 0, count); 73 count = fin.read(buffer); 74 } 75 fin.close(); 76 fout.close(); 77 System.out.println("Copy file from " + this.filename + " to " + filename2); 78 } 79 } 80 81 //數據字節流(舉例 整形) 82 //把fibonacci序列值寫入指定文件 83 static class IntFile{ 84 public String filename; 85 public IntFile(String filename){ 86 this.filename = filename; 87 } 88 public void writeToFile() throws IOException{ 89 90 FileOutputStream fout = new FileOutputStream(this.filename); 91 DataOutputStream dout = new DataOutputStream(fout); 92 short i=0,j=1; 93 do{ 94 dout.writeInt(i); 95 dout.writeInt(j); 96 i = (short)(i+j); 97 j = (short)(i+j); 98 }while(i>0); 99 dout.close(); 100 fout.close(); 101 } 102 public void readFromFile() throws IOException{ 103 FileInputStream fin = new FileInputStream(this.filename); 104 DataInputStream din = new DataInputStream(fin); 105 System.out.println(this.filename + ": "); 106 while(true){ 107 try { 108 int i = din.readInt(); 109 System.out.print(i + " "); 110 } catch (Exception e) { 111 break; 112 } 113 } 114 din.close(); 115 fin.close(); 116 } 117 } 118 119 //對象字節流 120 //使用對象字節流讀寫多個學生對象到某個指定文件 121 static class objectFile{ 122 String filename; 123 public objectFile(String filename){ 124 this.filename = filename; 125 } 126 127 public void writeToFile(Object[] objs) throws IOException{ 128 FileOutputStream fout = new FileOutputStream(this.filename); 129 ObjectOutputStream obout = new ObjectOutputStream(fout); 130 for(int i = 0; i<objs.length; i++){ 131 obout.writeObject(objs[i]); 132 } 133 obout.close(); 134 fout.close(); 135 } 136 137 public String readFromFile() throws IOException{ 138 FileInputStream fin = new FileInputStream(this.filename); 139 ObjectInputStream obin = new ObjectInputStream(fin); 140 System.out.println(this.filename + ": "); 141 String str = ""; 142 while(true){ 143 try { 144 str += obin.readObject().toString() + "\n"; 145 } catch (Exception e) { 146 break; 147 } 148 } 149 obin.close(); 150 fin.close(); 151 return str; 152 } 153 } 154 public static class Student implements Serializable{//內部類 學生對象 155 156 private String name; 157 private String sex; 158 public Student(String name, String sex){ 159 this.name = name; 160 this.sex = sex; 161 } 162 public String toString(){ 163 return "姓名: " + this.name + " 性別: " + this.sex; 164 } 165 } 166 }

javaIO--字節流