当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PrintStream write(byte[], int, int)用法及代码示例


Java中PrintStream Class的write(byte [],int,int)方法用于在流上写入指定的byteacter数组的指定部分。此byteacter数组用作参数。要写入的字节字符的起始索引和长度也被当作参数。

用法:

public void write(byte[] byteArray, int startingIndex, int lengthOfCharArray)



参数:此方法接受三个强制参数:

  • byteArray这是要在Stream中写入的byteacter数组。
  • startingIndex这是从中获取byteacter部分的起始索引。
  • lengthOfCharArray这是要写入流中的字节演员的长度。

返回值:此方法不返回任何值。

下面的方法说明了write(byte [],int,int)方法的用法方式:

程序1:

// Java program to demonstrate 
// PrintStream write(byte[], int, int) method 
  
import java.io.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            // Create a PrintStream instance 
            PrintStream stream 
                = new PrintStream(System.out); 
  
            // Get the byteacter array 
            // to be written in the stream 
            byte[] byteArray = { 65, 66, 67 }; 
  
            // Get the starting index 
            int startingIndex = 0; 
  
            // Get the length of byte 
            int lengthOfCharArray = 1; 
  
            // Write the portion of the byteArray 
            // to this stream using write() method 
            // This will put the byteArray in the stream 
            // till it is printed on the console 
            stream.write(byteArray, 
                         startingIndex, 
                         lengthOfCharArray); 
  
            stream.flush(); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
A

程序2:

// Java program to demonstrate 
// PrintStream write(byte[], int, int) method 
  
import java.io.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            // Create a PrintStream instance 
            PrintStream stream 
                = new PrintStream(System.out); 
  
            // Get the byteacter array 
            // to be written in the stream 
            byte[] byteArray = { 97, 98, 99 }; 
  
            // Get the starting index 
            int startingIndex = 2; 
  
            // Get the length of byte 
            int lengthOfCharArray = 1; 
  
            // Write the portion of the byteArray 
            // to this stream using write() method 
            // This will put the byteArray in the stream 
            // till it is printed on the console 
            stream.write(byteArray, 
                         startingIndex, 
                         lengthOfCharArray); 
  
            stream.flush(); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
输出:
c


相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 PrintStream write(byte[], int, int) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。