博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java DataOutputStream writeInt()方法及示例
阅读量:2529 次
发布时间:2019-05-11

本文共 2874 字,大约阅读时间需要 9 分钟。

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 DataOutputStreamimport 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): 10dos_stm.writeInt(val): 20dos_stm.writeInt(val): 30dos_stm.writeInt(val): 40dos_stm.writeInt(val): 50

翻译自:

转载地址:http://bbtzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>