Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class ReadWrite {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- try {
- RandomAccessFile rand = new RandomAccessFile("C:/Users/Caiden/Desktop/Stu.dat", "rw");
- for (int i=0;i<5;i++) {
- System.out.print("Student ID:");
- rand.writeInt(scan.nextInt());
- System.out.print("GPA:");
- rand.writeDouble(scan.nextDouble());
- }
- boolean terminate = false;
- while (!terminate) {
- System.out.println("Type ID to see GPA. Type 0 to exit.");
- int id = scan.nextInt();
- if (id == 0) {
- terminate = true;
- break;
- }
- rand.seek(0);
- int curr = 0;
- while (curr != -1 && curr != id) {
- if (rand.getFilePointer() != rand.length())
- curr = rand.readInt();
- else break;
- }
- if (curr == id) System.out.println("GPA: " + rand.readDouble());
- else System.out.println("Sorry! ID not found!");
- }
- } catch (IOException e) {
- System.out.println("IO error!");
- e.printStackTrace();
- } finally {
- System.out.println("Shutting down...");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement