Buffer and Object input and out putITeye - 乐橙lc8

Buffer and Object input and out putITeye

2019年02月27日12时07分50秒 | 作者: 昊天 | 标签: 目标,实例,编码 | 浏览: 1043

  The use of BufferedInputStream/BufferedOutputStream.
If we copy a file.
1.input the file
2.OS RAM
3.JVM RAM
4.code
5.JVM RAM
6.OS RAM
7.ouput the file
So many procedures,its why that the copy a file cost so much time.
If we use the buffer the files will fisrt store in the JVM RAM.Then send to the code at one time.
It just like a car send one package at one time.With buffer,you can use the car send many package at one time.So efficent it is.
public static void writeContent(String path) throws IOException{
 //实例化一个输出流目标
 FileOutputStream fos = new FileOutputStream(path,ture);
 BufferedOutputStream bos = new BufferedOutputStream(fos);
 String [] array = {"a","b","c","d","e","f"};
 //体系会自己给你一个默许的巨细缓冲空间,
 for(int i=0;i array.length;i++){
 bos.write(array[i].charAt(0)); //把字符串改成字符 
 System.out.println("文件写入成功!");
 bos.flush();
 bos.close();
 fos.close();
 }


//只能将支撑 java.io.Serializable 接口的目标写入流中。每个 serializable 目标的类都被编码,编码内容光辉类名和类签名、目标的字段值和数组值,以及从初始目标中引证的其他一切目标的闭包。

The serialization of the object
If we want to I/O a object.We should let the 
class interface serializable .
The java.io.ObjectInputStream classs void writeOject(Object obj) method to input a object.The same as Output.
Under some special condition,If you want to keep the classs attributes as secrets.Just add the key word transient(时间短的).The system wont store the attribute when input.
private static void writeObject(String string) throws IOException, ClassNotFoundException {
 student st=new student("张三",5);
 //实例化一个输出流目标
 FileOutputStream fos = new FileOutputStream("src/xy_进阶的IO流0909/student.txt");
 //实例化一个目标流目标
 ObjectOutputStream oos=new ObjectOutputStream(fos);
 //写入目标
 oos.writeObject(st);
 //强制写入目标流
 oos.flush();
 //封闭目标流
 oos.close();
 //实例化一个输入流目标
 FileInputStream fis = new FileInputStream("src/xy_进阶的IO流0909/student.txt");
 //实例化一个目标流目标
 ObjectInputStream ois=new ObjectInputStream(fis);
 //需求强制转型
 student st1=(student)ois.readObject();
 System.out.println(st1.getName());
 }
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章