Java Writer write(char[], int, int)方法及示例

来源:这里教程网 时间:2026-02-17 21:17:27 作者:

Java Writer write(char[], int, int)方法及示例

Java中Writer类的write(char[], int, int)方法是用来将指定的字符数组的指定部分写入写入器中。这个字符数组被作为一个参数。要写入的字符的起始索引和长度也被作为参数。

语法。

public void write(char[] charArray, int startingIndex, int lengthOfCharArray)

参数。这个方法接受三个强制性参数。

charArray是要写入写入器中的字符数组。startingIndex是起始索引,将从该索引中提取字符部分。lengthOfCharArray是要写入写入器的字符的长度。

返回值。这个方法不返回任何值。

下面的方法说明了write(char[], int, int)方法的工作。

程序1:

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

输出:

A

程序2。

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

输出:

c

相关推荐