javaIO--字节流
流---是指的一组有序的、有气垫和重点的字节集合,是对的护具传输的总称或者抽象。
流采用缓冲区技术,当写一个数据时,系统将数据发送到缓冲区而不是外部设备(如硬盘),当读一个数据时,系统实际是从缓冲区读取数据的。
流的存在:我们都知道输入流和输出流,二者的定义都是基于数据流向且是以内存为坐标的。标准输入过程中,数据从键盘等输入设备流向内存,这事输入流。标准输出过程中,数据从内存输出到显示器或者打印机等设备,这是输出流。
流分为字节流和字符流
下边主要讲标准输入输出、文件字节流、数据字节流和对象字节流四种字节流。
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 }