⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randomfile.java~3~

📁 java2参考大全上的例子的源码和自己的理解.
💻 JAVA~3~
字号:
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);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -