IO流的分析

字符流和字节流

  • 拷贝东西可以用字节流,但读取文本就要用字符流。因为中文和英文的所占字节不同,一个文本中可能包含中英混合,如果用字节流读取文本,会发生读取字节分离(在读取文本的时候是读取到一个byte数组里,但byte数组有一定大小,不能一下子读完,可能出现一个中文字符读取了一半。导致部份乱码),如果不牵涉到读取文本啥的可以用字节流。字节流可以读取任何文件。
  • 关于拷贝和读取的时候,可以借byte数组来当缓冲。java中的String类可以用byte组数创建新的string对象,如图所示
FileInputStream files=null;
        try {
        	 byte[] a=new byte[5];
			 files=new FileInputStream("src/com/ht/io/temple");
			 char onebyte=(char) files.read();
			 System.out.print(onebyte);
			 int readcount;
			 while((readcount=files.read(a))!=-1) {
				 System.out.print(new String(a,0,readcount));
			 }

-ObjectOutputStream和ObjectInputStream可以序列化对象,但对象要实现Serializable接口

SerializeTest.Student a=new SerializeTest.Student("zhangsan");
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("serialize"));
		out.writeObject(a);
		out.flush();
		out.close();
上一篇:img标签中的图片加载异常时显示默认的图片


下一篇:centos yum源失效报错配置修改