📄 randomfile.java~6~
字号:
package random_file;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
/*
例:把若干个32位的整数写到一个名为 “temp.dat”的文件中,然后利用seek方法,以相反的顺序再读取这些数据
*/
import java.io.*;
/*
public class RandomFile {
public static void main(String args[]) {
int data_arr[] = {
12, 31, 56, 23, 27, 1, 43, 65, 4, 99};
try {
RandomAccessFile randf = new RandomAccessFile("temp.dat", "rw");
for (int i = 0; i < data_arr.length; i++) {
randf.writeInt(data_arr[i]);
}
for (int i = data_arr.length - 1; i >= 0; i--) {
randf.seek(i * 4);
System.out.println(randf.readInt());
}
randf.close();
}
catch (IOException e) {
System.out.println("File access error: " + e);
}
}
}*/
public class RandomFile {
public static void main(String args[]) {
String data_arr[] = {
"12","31", "56", "23", "27", "1", "43", "65", "4", "99"};
try {
RandomAccessFile randf = new RandomAccessFile("temp.dat", "rw");
for (int i = 0; i < data_arr.length; i++) {
randf.writeChars(data_arr[i]);
}
for (int i = data_arr.length - 1; i >= 0; i--) {
randf.seek(i * 4);
System.out.println(randf.readLine());
}
randf.close();
}
catch (IOException e) {
System.out.println("File access error: " + e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -