java skipbytes_int skipBytes(int len)

本文介绍了Java `ObjectInputStream` 类的 `skipBytes` 方法,该方法用于在输入流中跳过指定数量的字节。示例代码展示了如何使用此方法读取文件并跳过部分字节,从而读取剩余内容。通过调用 `skipBytes(4)`,程序跳过前四个字节并打印后续内容。
摘要由CSDN通过智能技术生成

int skipBytes(int len)

描述 (Description)

java.io.ObjectInputStream.skipBytes(int len)方法跳过字节。

声明 (Declaration)

以下是java.io.ObjectInputStream.resolveObject()方法的声明。public int skipBytes(int len)

参数 (Parameters)

len - 要跳过的字节数。

返回值 (Return Value)

此方法返回跳过的实际字节数。

异常 (Exception)

IOException - 基础InputStream抛出的任何异常。

例子 (Example)

以下示例显示了java.io.ObjectInputStream.skipBytes()方法的用法。package com.iowiki;

import java.io.*;

public class ObjectInputStreamDemo {

public static void main(String[] args) {

String s = "Hello World!";

try {

// create a new file with an ObjectOutputStream

FileOutputStream out = new FileOutputStream("test.txt");

ObjectOutputStream oout = new ObjectOutputStream(out);

// write something in the file

oout.writeUTF(s);

oout.writeUTF("This is an example");

oout.flush();

// create an ObjectInputStream for the file we created before

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

// skip 4 bytes and read the rest

ois.skipBytes(4);

for (int i = 0; i < ois.available() - 4; i++) {

System.out.print("" + (char) ois.readByte());

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

让我们编译并运行上面的程序,这将产生以下结果 -llo World!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值