现在的位置: 首页 > 综合 > 正文

读写二进制文件

2018年12月11日 ⁄ 综合 ⁄ 共 2501字 ⁄ 字号 评论关闭
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


抱歉!评论已关闭.