Java(数据,字节数组)输入输出流

Java的数据输入输出流是非常强大的IO,使用它可以使我们的程序更加高效。它封装了很多的函数供我们使用。

import java.io.DataInputStream;
import java.io.DataOutputStream;
readInt();//读取一个整型
readChar();//读取一个字符
readFloat();//读取一个浮点数
readLine();//读取一行
readBoolean();//读取布尔型
readShort();//读取短整型

例如,我们可以直接通过以下方式读取一个整型数据

File file=new File("D:/io.txt");
FileInputStream fis=new FileInputStream(file);
 int ch1 = fis.read();//读取一个字节
 int ch2 = fis.read();
 int ch3 = fis.read();
 int ch4 = fis.read();
 int x=((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));

也可以使用封装好的数据流

File file=new File("D:/io.txt");
FileInputStream fis=new FileInputStream(file);
DataInputStream dis=new DataInputStream(fis);
int x=dis.readInt();//直接读取一个整型数据

可以看出,使用了数据输入输出流的代码更加简洁,让人一目了然。事实上,DataInputStream.readInt()内部的实现方法就是像第一段代码写的那样,你可能会问,这两者之间的效率可能并没有什么差别,但是在我们读取更多不同类型数据的时候,两者之间的差距就显而易见了

使用这种方式,我们可以轻松的实现数据的读写

public class Objects {

	public int integer;
	public byte bytes;
	public String  string;
    
}
public class WriteFile {

	public static void main(String [] args) throws Exception {
		int total_num=10;
		List<Objects> oblist= new ArrayList<Objects>();
		for(int i=0;i<total_num;i++) {
			Objects e=new Objects();
			e.integer=i+1;
			e.bytes=(byte)(i+1);
			e.string="数据等于="+(char)(i+48);
			oblist.add(e);		
		}
		//向文件中写入数据
		FileOutputStream fos=new FileOutputStream("C:\\Users\\14005\\Desktop\\test.txt");
		DataOutputStream dos=new DataOutputStream(fos);
		dos.writeInt(total_num);
		for(int i=0;i<total_num;i++) {			
			dos.writeInt(oblist.get(i).integer);
			dos.write(oblist.get(i).bytes);
			dos.write(oblist.get(i).string.getBytes());
			dos.flush();
		}
		fos.close();
		dos.close();
		//从文件中读取数据
		File file=new File("C:\\Users\\14005\\Desktop\\test.txt");
		FileInputStream fis=new FileInputStream(file);
		DataInputStream dis=new DataInputStream(fis);
		int length=dis.readInt();
		for(int i=0;i<length;i++) {
			int integer=dis.readInt()-1;
			byte bytes=(byte)(dis.readByte()-1);
			byte[] b=new byte[10];
			dis.read(b);
			System.out.println(integer+" "+bytes+" "+new String(b));
		}
		fis.close();
		dis.close();
	}

}

ByteArrayInputStream ByteArrayOutputStream
基本用法:

int LEN = 5;
byte[] ArrayLetters = {
    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
  };
private static void tesByteArrayInputStream() {
    // 创建ByteArrayInputStream字节流,内容是ArrayLetters数组
    ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
 
    // 从字节流中读取5个字节
    for (int i=0; i<LEN; i++) {
      // 若能继续读取下一个字节,则读取下一个字节
      if (bais.available() >= 0) {
        // 读取“字节流的下一个字节”
        int tmp = bais.read();
        System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
      }
    }

参考链接 https://www.jb51.net/article/86769.htm
ByteArrayInputStream,ByteArrayOutputStream在转化BufferedImage与[]byte时非常方便

public byte[] send_imagebyte(BufferedImage i) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bos.write(1);
			try {
				ImageIO.write(i, "jpg",bos);
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			byte[] bytes = bos.toByteArray();
			return bytes;
		}
public  BufferedImage To_Image(byte[] buffer)  {
		   ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
		   BufferedImage recvImg = null;
		   try {
			   recvImg = ImageIO.read(bis);
		   } catch (IOException e) {
			   e.printStackTrace();
		   }
		   return recvImg;
	   	}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值