java.io.RandomAccessFile.writeChar(int v) 方法写入一个字符的文件作为一个double字节值,高字节在前。写入从文件指针的当前位置。
声明
以下是java.io.RandomAccessFile.writeChar()方法的声明
public final void writeChar(int v)
参数
-
v -- 要写入一个char值。
返回值
此方法无任何返回值。
异常
-
IOException -- 如果发生I/O错误。
例子
下面的示例演示java.io.RandomAccessFile.writeChar()方法的用法。
package com.yiibai; import java.io.*; public class RandomAccessFileDemo { public static void main(String[] args) { try { int i = 70; // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write a char in the file raf.writeChar(i); // set the file pointer at 0 position raf.seek(0); // read char System.out.println("" + raf.readChar()); // set the file pointer at 0 position raf.seek(0); // write a char at the start raf.writeChar(71); // set the file pointer at 0 position raf.seek(0); // read char System.out.println("" + raf.readChar()); } catch (IOException ex) { ex.printStackTrace(); } } }
假设我们有一个文本文件c:/ test.txt,它具有以下内容。该文件将被用作输入到我们的示例程序:
ABCDE
让我们来编译和运行上面的程序,这将产生以下结果:
F
G