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


Java Java.io.Writer.write()用法及代码示例



描述

这个java.io.Writer.write(String str,int off,int len)方法写入字符串的一部分。

声明

以下是声明java.io.Writer.write()方法。

public void write(String str,int off,int len)

参数

  • str- 要写入的字符串。

  • off- 开始写入字符的偏移量。

  • len- 要写入的字符数。

返回值

此方法不返回值。

异常

  • IndexOutOfBoundsException− 如果 off 为负数,或 len 为负数,或 off+len 为负数或大于给定字符串的长度。

  • IOException− 如果发生 I/O 错误。

示例

下面的例子展示了使用java.io.Writer.write()方法。

package com.tutorialspoint;

import java.io.*;

public class WriterDemo {
   public static void main(String[] args) {
      String str = "Hello world!";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a string portion
         writer.write(str, 0, 5);

         // flush the writer
         writer.flush();

         // write another string portion
         writer.write(str, 5, 6);

         // flush the stream again
         writer.flush();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Hello world

相关用法


注:本文由纯净天空筛选整理自 Java.io.Writer.write() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。