Java DataOutputStream writeByte()方法与示例

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545282.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

聊聊近期的感受和10月文章精选!

先来看本月的原创文章汇总,其中算法部分也有少量 9 月份的文章,这样汇总起来大家看起来更方便,目录如下。算法系列小白学算法第1篇:一文详解「栈」和手撸栈的两种方法!小白学算法第2篇:JDK 竟然是这样实现栈…

第六章关系数据理论

第六章关系数据理论6.1_问题的提出(略)6.2_规范化6.2.1_函数依赖6.2.2_码6.2.3_范式6.2.4_2NF6.2.5_3NF6.2.6_BCNF6.2.7_多值依赖6.2.8_4NF6.2.9_规范化小结6.3_Armstrong公理系统6.3.1_函数依赖闭包6.3.2_最小依赖集6.3.3_转换为3NF的保持函数依赖的分解…

Oracle数据字典

--用户信息 select * from dba_users; --角色信息 select * from dba_roles; --权限信息 select * from dba_role_privs;--方案(Schema)是数据库对象的集合,每个用户都对应一个方案。用户是各种对象的所有者, --方案是对象的组织形式。 --用户与方案是一…

Github上的热门iOS开源项目:AFNetworking、MagicalRecord、BlocksKit以及XVim

1. AFNetworking AFNetworking是一个非常受欢迎的轻量级的iOS、Mac OS X网络通信类库。它建立在NSURLConnection、NSOperation以及其技术的基础上,有着精心设计的模块结构和功能丰富的API,让很多网络通信功能的实现变得十分简单。 附件:/c…

队列实现栈的3种方法,全都击败了100%的用户!

作者 | 王磊来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)本文已收录至 Github《小白学算法》系列:https://github.com/vipstone/algorith之前我们讲过《用两个栈实现一个…

Java类类getConstructor()方法及示例

类的类getConstructor()方法 (Class class getConstructor() method) getConstructor() method is available in java.lang package. getConstructor()方法在java.lang包中可用。 getConstructor() method is used to return a Constructor object that reflects the given pub…

where、having、group by、order by、count的使用注意

where、having、group by、order by、count的使用注意1_where、having、group by、order by的顺序2_group by的作用3_where和group by的组合4_group by和having的组合5_where、having、group by的组合使用6_count与group by的组合使用1_where、having、group by、order by的顺序…

Oracle备份还原

Oracle有两类备份方式: (1)物理备份:是将实际组成数据库的操作系统文件从一处拷贝到另一处的备份过程,通常是从磁盘到磁带。物理备份又分为冷备份、热备份; (2)逻辑备份&#xff1…

Eclipse快捷键-方便查找

【Ct rlT】 搜索当前接口的实现类 1. 【ALT /】 此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类、方法和属性的名字时,多体验一下【ALT /】快捷键带来的好处吧。 2. 【Ct rlO】…

很实用的21个SQL小技巧!

前言每一个好习惯都是一笔财富,本文基于MySQL,分SQL后悔药, SQL性能优化,SQL规范优雅三个方向,分享写SQL的21个好习惯,谢谢阅读,加油哈~1. 写完SQL先explain查看执行计划(SQL性能优化…

Java ByteArrayOutputStream size()方法与示例

ByteArrayOutputStream类size()方法 (ByteArrayOutputStream Class size() method) size() method is available in java.io package. size()方法在java.io包中可用。 size() method is used to return the current size of the buffer exists. size()方法用于返回缓冲区当前存…

Oracle备份文件名获取系统时间的做法(windows)

在对数据库进行备份时,用数据泵的方法,需要执行一段代码: --建目录 create directory dir as d:\bak;--给用户授权使用目录 grant read,write on directory dir to scott;--在cmd中输入以下代码 expdp scott/tigerXE directorydir dumpfileab…

java getmonth_Java LocalDateTime类| 带示例的getMonth()方法

java getmonthLocalDateTime类getMonth()方法 (LocalDateTime Class getMonth() method) getMonth() method is available in java.time package. getMonth()方法在java.time包中可用。 getMonth() method is used to get the field value month-of-year based on the enum Mon…

阿里最喜欢问的多线程顺序打印的5种解法!

Keeper导读大家在换工作面试中,除了一些常规算法题,还会遇到各种需要手写的题目,所以打算总结出来,给大家个参考。全文 2929 字,剩下的是代码,P6 及以下阅读只需要 8 分钟,高 P 请直接关闭第一篇…

CSS入门

CSS入门1_CSS概要1.1_CSS引入方式2_CSS选择器3_字体样式3.1_字体属性3.2_字体类型:font-family3.3_字体大小:font-size3.4_字体粗细:font-weight3.5_字体颜色:color3.6_总结4_文本样式4.1_文本样式属性4.2_首行缩进:te…

Oracle笔记:数据库启动的三个阶段

数据库的启动可分为三个阶段:1、startup nomount -- 启动实例,不加载数据库 nomount:在这一阶段,只需要读取initSID.ora文件,启动数据库实例,创建后台进程。在initSID.ora文件中,可以定位…

Java LocalDate类| lengthOfYear()方法和示例

LocalDate类lengthOfYear()方法 (LocalDate Class lengthOfYear() method) lengthOfYear() method is available in java.time package. lengthOfYear()方法在java.time包中可用。 lengthOfYear() method is used to get the length of the year represented by this LocalDate…

23张图!万字详解「链表」,从小白到大佬!

作者 | 王磊来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)链表和数组是数据类型中两个重要又常用的基础数据类型。数组是连续存储在内存中的数据结构,因此它的优势是可以通…

何为“秒传”

写在前面 最近一直在弄文件传输的组件,在讨论组里面,有同事提到“秒传”的功能。在目前的通信软件中也有网盘的功能,就从网上搜了一下,这里对“秒传”的实现思路做一下总结,在之后会写一个小的demo实现一下。没有其他&…

JavaScript入门

JavaScript入门1_语法基础1.1_变量1.2_数据类型1.3_运算符1.4_类型转换1.5_注释跟c一样2_流程控制语句3_函数3.1_没有返回值的函数3.2_有返回值的函数4_对象4.1_对象简介4.2_字符串对象4.3_数组对象4.4_时间对象4.5_数学对象简介5_DOM基础5.1_DOM是什么5.2_节点类型5.3_获取元素…