Java DataOutputStream writeInt()方法及示例

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

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

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

  • writeInt() method is used to write the given integer value to the basic DataOutputStream as 4 bytes (i.e. 32 bit) and the variable counter is plus by 4 on successful execution.

    writeInt()方法用于将给定的整数值作为4个字节(即32位)写入基本DataOutputStream,并且成功执行时变量计数器加4。

  • writeInt() 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.

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

  • writeInt() method may throw an exception at the time of writing an integer.

    writeInt()方法在写入整数时可能会引发异常。

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

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

Syntax:

句法:

    public final void writeInt(int val);

Parameter(s):

参数:

  • int val – represents the integer value to be written to the 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 writeInt(int val) method 
// of DataOutputStream
import java.io.*;
public class WriteIntOfDOS {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
DataInputStream dis_stm = null;
FileOutputStream fos_stm = null;
DataOutputStream dos_stm = null;
int[] in = {
10,
20,
30,
40,
50
};
try {
// Instantiate FileInputStream, 
// DataInputStream, FileOutputStream
// and DataOutputStream 
fos_stm = new FileOutputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
dos_stm = new DataOutputStream(fos_stm);
is_stm = new FileInputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
dis_stm = new DataInputStream(is_stm);
for (int val: in ) {
// By using writeInt() method isto
// write int value to the
// DataOutputStream dos_stm
dos_stm.writeInt(val);
}
// By using flush() method isto
// flush the bytes to the basic 
// output stream         
dos_stm.flush();
// Loop To Read Available Data till end
while (dis_stm.available() > 0) {
// By using readInt() method isto read 
// int from dis_stm
int val = dis_stm.readInt();
System.out.println("dos_stm.writeInt(val): " + val);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// To free system resources linked
// with these streams
if (is_stm != null)
is_stm.close();
if (dis_stm != null)
dis_stm.close();
if (dos_stm != null)
dos_stm.close();
if (fos_stm != null)
fos_stm.close();
}
}
}

Output

输出量

dos_stm.writeInt(val): 10
dos_stm.writeInt(val): 20
dos_stm.writeInt(val): 30
dos_stm.writeInt(val): 40
dos_stm.writeInt(val): 50

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

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

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

相关文章

python安卓自动化实现方法_uiautomator +python 实现安卓UI自动化

简单实例注:安卓6.0以上的手机不会自动安装app-uiautomator.apk和app-uiautomator-test.apk,需要手动安装,否则报错ioerror RPC server not starteduiautomator pythonHTMLTestRunner 安卓UI自动化实现#coding:utf-8from uiautomator importD…

ES6特性之:Spread操作符

Spread操作符(...),也称作展开操作符,作用是将可迭代的(Iterable)对象进行展开。 比如有2个数组,我们要将其中一个数组中所有元素插入到另一个数组中,通过Spread操作符,就可以这样进行: var fruits ["…

Java类class isMemberClass()方法及示例

类的类isMemberClass()方法 (Class class isMemberClass() method) isMemberClass() method is available in java.lang package. isMemberClass()方法在java.lang包中可用。 isMemberClass() method is used to check whether the underlying class is a member class or not.…

velocity自定义函数_velocity基本语法和总结

一:基本语法:1、#set(#a "a")$a ##输出语句时直接写变量的名称即可2、判断语句:#if($a "a") ##判断语句没有括号,也是直接输出$a3、数组:#set($arry [0..10])$foreach($i in $arry)$i ##换行#e…

docker-machine指定cpu个数

序 给本机的一个服务压测,结果半天qps上不了万,而且经常跑满cpu,搞半天发现,docker里头才1核1G内存。原来boot2docker默认给docker-machine分配1个cpu和1G内存。 修改配置 docker-machine create \--driver virtualbox \--virtual…

Java ClassLoader findResources()方法与示例

ClassLoader类findResources()方法 (ClassLoader Class findResources() method) findResources() method is available in java.lang package. findResources()方法在java.lang包中可用。 findResources() method is used to find all the resources with the given resource …

Java ByteArrayInputStream mark()方法与示例

ByteArrayInputStream类mark()方法 (ByteArrayInputStream Class mark() method) mark() method is available in java.util package. mark()方法在java.util包中可用。 mark() method is used to set the current mark position in the stream from where read or write can b…

java mediainfo.dll_MediaInfo库的简单使用

想到一个问题, 如何获得一个图像文件(比如jpg, bmp, png)的信息. 自己查查文件的格式, 写一个解析, 应该不困难; 但是找了下现成的, 发现MediaInfo库已经可以非常好的实现需要的功能了.MediaInfo可以在sourceforge上找到, 是一个解析视频,音频, 图片等媒体文件的库. 可以得到文…

Redis配置和常用命令

1 redis.conf配置文件:2 引用3 #是否作为守护进程运行4 daemonize yes5 #配置pid的存放路径及文件名,默认为当前路径下6 pidfile redis.pid7 #Redis默认监听端口8 port 63799 #客户端闲置多少秒后,断开连接 10 timeout 300 11 #日志显示级别 …

oracle中dbms_DBMS中的功能依赖性和属性关闭

oracle中dbms功能依赖 (Functional Dependency) A relational Database management System (RDBMS) represents the database o a collection of relations/tables. A functional dependency is a constraint between two sets of attributes in a relation. It is the propert…

java invoke 泛型_利用Java反射机制和泛型,全自动解析json

有啦这个简直,太爽啦,利用Java 反射机制,利用Class 就可以得到 类的 变量 Field[] fieldscls.getDeclaredFields();还可以通过类中 的方法名字 去执行这个方法m1 cls.getDeclaredMethod(getMothodName(fields[j].getName()), String.class)…

2_C语言中的数据类型 (四)整数与无符号数

1.1 sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1 int类型 1.1.1 int常量,变量 int就是32位的一个二进制整数,在内存当中占据4个字节…

python 示例_Python TextCalendar类别| pryear()方法与示例

python 示例Python TextCalendar.pryear()方法 (Python TextCalendar.pryear() Method) pryear() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars. It uses an instance of TextCalendar class and prints …

Spring实战——通过Java代码装配bean

上篇说的是无需半行xml配置完成bean的自动化注入。这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果。 这么说,是要把上篇的料拿出来再煮一遍? 当然不是,上篇我们几乎都在用注解的方式如ComponentScan Component等就完成了…

java 谓词_java8-谓词(predicate)

传递代码我们首先看一个例子,假设你有一个 Apple 类,它有一个getColor方法,还有一个变量inventory保存着一个Apples的列表。你可能想要选出所有的绿苹果,并返回一个列表。通常我们用筛选(filter)一词来表达这个概念。在 Java 8之前…

getlong_Java LocalDateTime类| 带示例的getLong()方法

getlongLocalDateTime类的getLong()方法 (LocalDateTime Class getLong() method) getLong() method is available in java.time package. getLong()方法在java.time包中可用。 getLong() method is used to get the value as long for the given temporal field from this dat…

java.io和util的区别_Java NIO与IO的区别和比较

Java NIO与IO的区别和比较导读J2SE1.4以上版本中发布了全新的I/O类库。本文将通过一些实例来简单介绍NIO库提供的一些新特性:非阻塞I/O,字符转换,缓冲以及通道。一. 介绍NIONIO包(java.nio.*)引入了四个关键的抽象数据类型,它们共…

Java LocalDate类| isSupported()方法与示例

LocalDate类isSupported()方法 (LocalDate Class isSupported() method) Syntax: 句法: public boolean isSupported (TemporalField t_field);public boolean isSupported (TemporalUnit t_unit);isSupported() method is available in java.time package. isSupp…

区块链+税务的思考

2016年,区块链技术火了!各大金融公司、互联网巨头都竞相参加到区块链技术的研究中。我们公司的业务是税务的信息化领域,也希望通过区块链技术的应用,来提升为财税领域的服务。 区块链技术优缺点总结 下图是对区块链技术的一些特点…