因为我不知道如何轻松生成DataInputStream,我无法测试但是在这里提出我的问题。我使用Java1.7
我需要操作到达的DataInputStream,跳过一些字节,多次调用readShort()并跳过另一个字节。我想知道: Q1,如果我们在跳过后丢失原始DataInputStream 我调用readShort()之后的Q2,流会移动并指向下一个位置还是保持在同一位置
答案 0 :(得分:2)
如果你想跳过一些你可以使用的字节 1.InreadByte()方法的DataInputStream并创建一个n大小的for循环来跳过n个字节,之后 2.call readShort()方法从输入流中读取短值 3.之后再次调用readByte()再次跳过字节。
随时提出您的疑问
答案 1 :(得分:0)
我认为我没有完全得到你的问题,但生病了2个例子,一个用于
readShort(),它读取两个输入字节并返回一个短值。
和
skipBytes(int n)方法,它从跳过n个字节的数据 输入流。跳过后,您将无法再次阅读
readShort()
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
public static void main(String[] args) throws IOException {
InputStream is = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
short[] s = {12982,3568};
try{
// create file output stream
fos = new FileOutputStream("c:\\test.txt");
// create data output stream
dos = new DataOutputStream(fos);
// for each short in short buffer
for(short j:s)
{
// write short to data output stream
dos.writeShort(j);
}
// force data to the underlying file output stream
dos.flush();
// create file input stream
is = new FileInputStream("c:\\test.txt");
// create new data input stream
dis = new DataInputStream(is);
// available stream to be read
while(dis.available()>0)
{
// read two bytes from data input, return short
short k = dis.readShort();
// print short value
System.out.print(k+" ");
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}finally{
// releases all system resources from the streams
if(is!=null)
is.close();
if(dis!=null)
dis.close();
if(fos!=null)
fos.close();
if(dos!=null)
dos.close();
}
}
}
输出:12982 3568
的skipBytes()
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
public static void main(String[] args) throws IOException {
InputStream is = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
byte[] b = {4,124,119,114,125,45,76,83,84};
try{
// create file output stream
fos = new FileOutputStream("c:\\test.txt");
// create data output stream
dos = new DataOutputStream(fos);
// for each byte in buffer
for(byte j:b)
{
// write byte to the output stream
dos.writeByte(j);
}
// force data to the underlying file output stream
dos.flush();
// create file input stream
is = new FileInputStream("c:\\test.txt");
// create new data input stream
dis = new DataInputStream(is);
// available stream to be read
while(dis.available()>0)
{
// reads characters encoded with modified UTF-8
int k = dis.read();
// print
System.out.print(k+" ");
// skips 1 by
dis.skipBytes(1);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}finally{
// releases all system resources from the streams
if(is!=null)
is.close();
if(dis!=null)
dis.close();
if(fos!=null)
fos.close();
if(dos!=null)
dos.close();
}
}
}
输出:4 119 125 76 84