DataOutputStream类writeByte()方法 (DataOutputStream Class writeByte() method)

  • writeByte() method is available in java.io package.

    writeByte()方法在java.io包中可用。

  • writeByte() method is used to write the given value as one-byte value to the basic stream and the variable counter is plus by 1 on successful execution.

    writeByte()方法用于将给定值作为一字节值写入基本流,并且成功执行时变量计数器加1。

  • writeByte() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    writeByte()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • writeByte() method may throw an exception at the time of writing byte.

    writeByte()方法在写入字节时可能会引发异常。

    IOException: This exception may throw while getting any input/output error.

    IOException :在获取任何输入/输出错误时,可能引发此异常。

Syntax:

句法:

    public final void writeByte(int val);

Parameter(s):

参数:

  • int val – represents the byte value to be written to the basic data output stream.

    int val –表示要写入基本数据输出流的字节值。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example
// of void writeByte(int val) method
// of DataOutputStream
import java.io.*;
public class WriteByteOfDOS {public static void main(String[] args) throws Exception {ByteArrayOutputStream baos_stm = null;
DataOutputStream dos_stm = null;
byte[] b_arr = {10,
20,
30,
40,
50
};
try {// Instantiates ByteArrayOutputStream, DataOutputStream
baos_stm = new ByteArrayOutputStream();
dos_stm = new DataOutputStream(baos_stm);
for (byte val: b_arr) {// By using writeByte() method isto write
// byte value from the given byte array
// to the dos_stm stream
dos_stm.writeByte(val);
}
// By using flush() method isto
// flush output bytes are written out
// to the basic output stream
dos_stm.flush();
// By using toByArray() method isto
// convert the stream baos_stm to byte
// array
byte[] by = baos_stm.toByteArray();
// Loop to display each byte to the
// baos_stm data till its end
for (byte val: by) {// Display byte
System.out.println("dos_stm.writeByte(): " + val);
}
} catch (Exception ex) {System.out.println(ex.toString());
} finally {// this block is to free all necessary system
// resources linked with the stream
if (baos_stm != null)
baos_stm.close();
if (dos_stm != null)
dos_stm.close();
}
}
}

Output

输出量

dos_stm.writeByte(): 10
dos_stm.writeByte(): 20
dos_stm.writeByte(): 30
dos_stm.writeByte(): 40
dos_stm.writeByte(): 50

翻译自: https://www.includehelp.com/java/dataoutputstream-writebyte-method-with-example.aspx

Java DataOutputStream writeByte()方法与示例相关推荐

  1. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  2. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  3. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  4. catalog java,Java Connection getCatalog()方法与示例

    通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息.而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表. Connection接口的getCata ...

  5. filepermission java,Java FilePermission getActions()方法与示例

    FilePermission类getActions()方法getActions()方法在java.io包中可用. getActions()方法用于检查此FilePermission和给定对象在路径名和 ...

  6. java方法参数Bundle,Java ResourceBundle keySet()方法及示例

    ResourceBundle类keySet()方法keySet()方法在java.util包中可用. keySet()方法用于从此ResourceBundle及其超级捆绑包中获取所有现有键,以在Set ...

  7. java exec waitfor,Java Process waitFor()方法与示例

    流程类waitFor()方法在java.lang包中提供了waitFor()方法. waitFor()方法用于使当前正在运行的线程在需要时等待,直到由该Process对象表示的进程完成其终止为止. 当 ...

  8. java arraylist.add(),Java ArrayList add()方法与示例

    ArrayList类add()方法 语法:public boolean add(T ele); public void add(int indices, T ele);add()方法在java.uti ...

  9. java rollback用法,Java Connection rollBack()方法与示例

    回滚操作将撤消当前事务所做的所有更改,即,如果调用Connection接口的rollBack()方法,则所有修改都将还原到最后一次提交. 您还可以通过将所需的Savepoint对象作为参数传递给此方法 ...

最新文章

  1. pandas的series和dataframe
  2. js调试之console.log()
  3. 网络推广专员如何通过社交能力扩宽外链渠道助力网络推广?
  4. CVPR2017: Learning Deep Context-aware Features over Body and Latent Parts for
  5. 总结————AJAX应用的五个步骤:
  6. 【Kafka】Kafka使用代码设置offset值
  7. 【hackerrank】Week of Code 26
  8. 从 DCG 到 IDCG 再到 NDCG
  9. 专线维护 07/11
  10. 编程必会的100个代码大全,建议收藏
  11. Wordpress 网站搭建及性能监控方法详解!
  12. 谷歌翻译停服后,chrome无法自动翻译?解决办法来了~
  13. cinta作业6:拉格朗日定理
  14. 微信打开链接提示用浏览器打开
  15. 互联网摸鱼日报(2022-11-07)
  16. input框如何设置框禁止输入
  17. ARM汇编之MOV指令
  18. ios html加载css样式,iOS加载HTML, CSS代码
  19. MySQL load data 快速导入大批量数据
  20. tensorflow2.0 新特性 + kaggle练习

热门文章

  1. https开头的网址是什么意思_我想打这个面试官,他给我挖坑,问我:URI中的 “//” 有什么用?...
  2. python语言保留字的特点_python保留字及其说明
  3. oracle预定义异常
  4. 【转】利用python的KMeans和PCA包实现聚类算法
  5. 左偏树 P3377【模板】左偏树(可并堆)
  6. 网络框架 Retrofit(三)
  7. 运行Hadoop自带的wordcount单词统计程序
  8. [看书笔记]《深入java虚拟机》——java体系结构(二)
  9. 【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验
  10. 【Java】PMD规则学习(1) --字符串比较