读写二进制文件

这篇博客展示了如何使用Java进行二进制文件的读写操作,包括将整数、浮点数和双精度浮点数写入文件,以及从文件中读取这些数值。代码示例详细地说明了如何利用ByteBuffer进行数据转换和文件流处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io._
import java.nio._

def writeInts(out:OutputStream, a:Array[Int], buf:ByteBuffer, n: Int) {
    var nwritten = 0;
    val ibuff = buf.asIntBuffer
	val bbuff = buf.array
	while (nwritten < n) {
		val todo = if (n-nwritten > ibuff.capacity) ibuff.capacity else (n - nwritten)
		ibuff.put(a, nwritten, todo)
		ibuff.position(0)
		out.write(bbuff,0,todo*4)
		nwritten += todo
	}
}

def writeFloats(out:OutputStream, a:Array[Float], buf:ByteBuffer, n:Int) {
	var nwritten = 0
	val fbuff = buf.asFloatBuffer
	val bbuff = buf.array
	while (nwritten < n) {
		val todo = if (n - nwritten > fbuff.capacity) fbuff.capacity else (n - nwritten)
		fbuff.put(a, nwritten, todo)
		fbuff.position(0)
		out.write(bbuff, 0, todo*4)
		nwritten += todo
	}
}

def writeDoubles(out:OutputStream, a:Array[Double], buf:ByteBuffer, n:Int) {
	var nwritten = 0
	val dbuff = buf.asDoubleBuffer
	val bbuff = buf.array
	while (nwritten < n) {
		val todo = if (n - nwritten > dbuff.capacity) dbuff.capacity else n - nwritten
		dbuff.put(a, nwritten, todo)
		dbuff.position(0)
		out.write(bbuff, 0, 8*todo)
		nwritten += todo
	}
}

//read n ints to a, buf is a buffer
//instream --> buffer --> Array
def readInts(in:InoutStream, a:Array[Int], buf:ByteBuffer, n:Int) {
	val nread = 0//a has read ints from buff
	val ibuff = buf.asIntBuffer
	val bbuff = buf.array
	var readnow = 0// buf has read n bytes from instream
	while (nread < n) {
		val todo = if (n - nread > ibuff.capacity) ibuff.capacity else (n - nread) //for instants, need read 5 ints, 20 bytes, but buff only  has 18 bytes, so first time only read 16 bytes 
		readnow += in.read(bbuff, readnow, todo*4 - readnow) //last time 'readnow' bytes hasnt put in array, but remain in the head of buff
		ibuff.get(a, nread, readnow/4) //put and convert bytes in to ints to array
		ibuff.position(0) //get will get bytes from position
		nread += readnow/4
		if (readnow %4 ! =0) {
			System.arraycopy(bbuff, 4*(readnow/4), bbuff, 0, readnow%4)
		}
		readnow = readnow%4
		
	}
}

def writeBin() {
	val bos = new BufferedOutputStream(new FileOutputStream("tsmat"), 1024)
	val head  = new Array[Int](4)
	val tbuf = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN)
	val rows = 4
	val cols = 5
	val nnz = 8
	head(0) = 231
	head(1) = rows
	head(2) = cols
	head(3) = nnz //nnz
	writeInts(bos, head, tbuf, 4)

	val buff = ByteBuffer.allocate(1024).order(ByteOrder.LITTLE_ENDIAN)
	val colv = Array(0,1,2,4,6,8)
	val rowv = Array(2,1,1,2,2,3,0,2)
	val data = Array(0.3826f,0.5757f,0.2343f,0.4599f,0.5948f,0.6836f,0.49744f,0.92241f)

	writeInts(bos, colv, buff,cols+1)
	writeInts(bos, rowv, buff, nnz)
	writeFloats(bos, data, buff, nnz)
	bos.close
}

def readBin() {
	val bis = new BufferedInputStream(new FileInputStream("s45.1"), 1024)
	val buff = ByteBuffer.allocate(1024).order(ByteOrder.LITTLE_ENDIAN)
	val bbuff = buff.array
	val len = bis.read(bbuff,0,1024)
	println("len = " + len)
	for (i<-1 to 17) print(buff.getInt + " ")
	for (i<-1 to (len/4-17)) print(buff.getFloat + " ")
	bis.close
}



writeBin



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值