`

Buffer and Object input and out put

    博客分类:
  • IO
阅读更多
  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,it's 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 class's void writeOject(Object obj) method to input a object.The same as Output.
Under some special condition,If you want to keep the class's attributes as secrets.Just add the key word transient(短暂的).The system won't 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());
				
	}
分享到:
评论

相关推荐

    数位板压力测试

    • How hard is it to learn the interface and write a simple program that uses tablet input? • Can programmers of complex applications control the features they need? • Are more powerful tablet ...

    计算机网络第六版答案

    exploiting the buffer overflow vulnerability that might exist in an application). After finding the vulnerability, the attacker needs to scan for hosts that are vulnerable. The target is basically to...

    Sakemail

    Goeran Strehl (asem) has sended me a patch that fix a memory leak and one problem with the object inspector and the Text property of a SakMsg. Dmitry Bondarenko say that some servers do not send the ...

    servlet2.4doc

    The default behavior of this method is to call flushBuffer() on the wrapped response object. FORM_AUTH - Static variable in interface javax.servlet.http.HttpServletRequest String identifier for Form...

    Vue.js上传图片到阿里云OSS存储的方法示例

    - 调用客户端的`put`方法,传入文件对象和目标存储路径(object-name),进行文件上传。 - 上传成功后,获取返回的URL,可用于展示或保存到数据库。 3. **具体实现** - 安装ali-oss库:`npm install ali-oss --...

    带附件的webservice

    while ((n = input.read(buffer)) != -1) { fos.write(buffer, 0, n); System.out.println(buffer); } input.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } return name + ...

    java练习题

    queue.put(count++); System.out.println("Produced: " + count); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } } public static class Consumer implements ...

    Bochs - The cross platform IA-32 (x86) emulator

    - NE2K: Fixed "send buffer" command issue on big endian hosts - USB - converted common USB code plus devices to the new 'usb_common' plugin Now the USB device classes no longer exist twice if both...

    TensorFlow-Android:示例项目如何使用自定义模型在Android中实现TensorFlow

    inputs.put(inputTensorIndex[0], inputBuffer); Map, Object&gt; outputs = new HashMap(); tfliteModel.run(inputs, outputs); ByteBuffer outputBuffer = (ByteBuffer) outputs.get(outputTensorIndex[0]); ``` `...

    IBM MQ错误码大全

    2003: Unit of work encountered fatal error or backed out.** - **含义**:工作单元遇到了致命错误或被回滚。 - **解决方案**:检查程序代码中的事务处理逻辑,确保所有操作都正确地参与了事务。 **5. 2004: ...

    Struts2+ireport PDF报表

    &lt;param name="inputName"&gt;reportStream &lt;param name="bufferSize"&gt;1024 ``` 这里的`ReportAction`是我们自定义的Action,它负责处理生成报表的逻辑。 **iReport的使用** 在iReport中,我们可以设计报表模板...

Global site tag (gtag.js) - Google Analytics