目录

void writeBytes(String s)

描述 (Description)

java.io.RandomAccessFile.writeBytes(String s)方法将字符串作为字节序列写入文件。 通过丢弃其高8位,按顺序写出字符串中的每个字符。 写入从文件指针的当前位置开始。

声明 (Declaration)

以下是java.io.RandomAccessFile.writeBytes()方法的声明。

public final void writeBytes(String s)

参数 (Parameters)

s - 要写入的字节串。

返回值 (Return Value)

此方法不返回值。

异常 (Exception)

IOException - 如果发生I/O错误。

例子 (Example)

以下示例显示了java.io.RandomAccessFile.writeBytes()方法的用法。

package com.iowiki;
import java.io.*;
public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");
         // write bytes in the file
         raf.writeBytes("Hello World");
         // set the file pointer at 0 position
         raf.seek(0);
         // read line
         System.out.println("" + raf.readLine());
         // set the file pointer at 0 position
         raf.seek(0);
         // write bytes at the start
         raf.writeBytes("This is an example");
         // set the file pointer at 0 position
         raf.seek(0);
         // read line
         System.out.println("" + raf.readLine());
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

假设我们有一个文本文件c:/test.txt ,它具有以下内容。 该文件将用作示例程序的输入 -

ABCDE  

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

Hello World
This is an example
↑回到顶部↑
WIKI教程 @2018