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

📄 readrandom.java

📁 Java学习源代码检索系统免费版
💻 JAVA
字号:
//==============================================================
// ReadRandom.java - Reads typed data using random access
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import java.io.*;

public class ReadRandom {

 public static String readLine()
  throws IOException {
  BufferedReader br = 
   new BufferedReader(new InputStreamReader(System.in));
  return br.readLine();
 }

 // Prompt user for record number
 public static int getRecordNumber()
  throws IOException, NumberFormatException {
   System.out.print("Record number (-1 to quit)? ");
   return Integer.parseInt(readLine());
 }

 // Main program method
 public static void main(String args[]) {
  // Instance variables
  int dataSize;         // Number of elements in file
  int rn;               // Record number
  double value;         // Value of requested record
  int sizeOfInt = 4;    // Size of int variable
  int sizeOfDouble = 8; // Size of double variable  
  boolean wantsToQuit = false;
  try {
   // Create file objects
   File fi = new File("Data.bin");
   RandomAccessFile rin = new RandomAccessFile(fi, "r");
   dataSize = rin.readInt();     // Get number of elements
   // Prompt user for element to read
   System.out.println("\nFile has " + 
    dataSize + " elements\n");
   while (!wantsToQuit) {
    rn = getRecordNumber();
    wantsToQuit = (rn == -1);
    if (!wantsToQuit) {
     // Seek to requested record
     rin.seek(sizeOfInt + (rn * sizeOfDouble));
     // Read and display value
     value = rin.readDouble();
     System.out.println("Record " + rn + " = " + value);
    }
   }
  } catch (IOException e) {            // Trap exception
   System.err.println(e.toString());   // Display error
  }
 }
}

⌨️ 快捷键说明

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