📄 randomfile.java
字号:
/** \u00A9 2005 Aptech Limited
* 版权所有
*/
/** 创建一个包. */
package random;
/* 导入 RandomAccessFile 类. */
import java.io.RandomAccessFile;
/* 这个类演示 RandomAccessFile.
* @version 1.0, 2005 年 6 月 13 日
* @author Ben
*/
/**
*
* <p>Title: RandomAccessFile Demo</p>
*
* <p>Description: Program forwriting and then reading file</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Aptech Limited</p>
*
* @author not attributable
* @version 1.0
*/
public class RandomFile {
/** 构造函数. */
protected RandomFile() {
}
/** 这个方法编写一个文件,并逆向读取内容. */
final void readwriteFile() {
int []numbers = {12, 31, 56, 23, 27, 1, 43, 65, 4, 99};
try {
RandomAccessFile objRandom = new RandomAccessFile("rand.dat", "rw");
for (int i = 0; i < numbers.length; i++) {
objRandom.writeInt(numbers[i]);
}
System.out.println("逆向读取给定数组的内容…");
for (int i = numbers.length - 1; i >= 0; i--) {
objRandom.seek(i * 4);
System.out.println(objRandom.readInt());
}
objRandom.close();
} catch (Exception n) {
System.out.println("生成的异常:" + n);
} finally {
System.out.println("始终在最后执行…");
System.out.println("在 finally 语句块内");
}
}
/** 这是 main 方法.
* 它创建 RandomFile 类的对象并访问其方法
* @param args 被传递至 main 方法
*/
public static void main(final String []args) {
RandomFile objRandomFile = new RandomFile();
objRandomFile.readwriteFile();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -