import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.io.IOException; import java.io.RandomAccessFile; public class Address extends Application { private RandomAccessFile raf; private String filePath = "Test.dat"; private TextField tfname = new TextField(); private TextField tfstreet = new TextField(); private TextField tfcity = new TextField(); private TextField tfstate = new TextField(); private TextField tfzip = new TextField(); private long currPos = 0;//当前指针位置 private int pageIndex = 0;//当前页数 private int byteLen = 101;//一段英文的字节总长度 中文和英文的字节长度不同,目前只支持英文 public static void main(String[] args) { // TODO Auto-generated method stub Application.launch(args); new Address().init(); } @Override public void start(Stage stage) { // TODO Auto-generated method stub BorderPane borderpane = new BorderPane(); GridPane pane = new GridPane(); pane.setPadding(new Insets(11, 12, 13, 14)); pane.setHgap(5); pane.setVgap(5); pane.setAlignment(Pos.CENTER_LEFT); pane.add(new Label("Name"), 0, 0); tfname.setPrefWidth(295); pane.add(tfname, 1, 0); pane.add(new Label("Street"), 0, 1); tfstreet.setPrefWidth(295); pane.add(tfstreet, 1, 1); HBox box1 = new HBox(); box1.setAlignment(Pos.CENTER); box1.setSpacing(5); box1.getChildren().addAll(new Label("City")); tfcity.setPrefWidth(160); box1.getChildren().addAll(tfcity); box1.getChildren().addAll(new Label("State")); tfstate.setPrefWidth(30); box1.getChildren().addAll(tfstate); box1.getChildren().addAll(new Label("Zip")); tfzip.setPrefWidth(50); box1.getChildren().addAll(tfzip); HBox box2 = new HBox(); box2.setSpacing(10); box2.setPadding(new Insets(5, 5, 5, 5)); box2.setAlignment(Pos.CENTER); Button add = new Button("Add"); box2.getChildren().add(add); add.setOnAction(e -> addAddress()); Button first = new Button("First"); box2.getChildren().add(first); first.setOnAction(e -> firstAddress()); Button next = new Button("Next"); box2.getChildren().add(next); next.setOnAction(e -> nextAddress()); Button previous = new Button("Previous"); box2.getChildren().add(previous); previous.setOnAction(e -> previousAddress()); Button last = new Button("Last"); box2.getChildren().add(last); last.setOnAction(e -> lastAddress()); Button update = new Button("Update"); box2.getChildren().add(update); update.setOnAction(e -> updateAddress()); borderpane.setTop(pane); borderpane.setCenter(box1); borderpane.setBottom(box2); Scene scene = new Scene(borderpane, 380, 150); stage.setScene(scene); stage.setTitle("地址簿"); stage.show(); } public void init() { try { raf = new RandomAccessFile(filePath, "rw"); //raf.setLength(0); //清空内容 System.out.println("文件字节总长度:" + raf.length()); System.out.println("文件总页数:" + raf.length() / byteLen); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private void firstAddress() { // TODO Auto-generated method stub try { if (raf.length() == 0) { return; } pageIndex = 0; currPos = pageIndex * byteLen; System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "first", currPos, pageIndex + 1)); raf.seek(currPos); readAddress(); } catch (IOException e) { e.printStackTrace(); } } private void nextAddress() { // TODO Auto-generated method stub try { if (raf.length() == 0) { return; } if (pageIndex < (raf.length() / byteLen - 1)) { pageIndex ++; currPos = pageIndex * byteLen; System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "next", currPos, pageIndex + 1)); raf.seek(currPos); readAddress(); } } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private void previousAddress() { // TODO Auto-generated method stub try { if (raf.length() == 0) { return; } if (pageIndex != 0) { pageIndex --; currPos = pageIndex * byteLen; System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "previous", currPos, pageIndex + 1)); raf.seek(currPos); readAddress(); } } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private void lastAddress() { // TODO Auto-generated method stub try { if (raf.length() == 0) { return; } pageIndex = (int) raf.length() / byteLen - 1; currPos = pageIndex * byteLen; System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "last", currPos, pageIndex + 1)); raf.seek(currPos); readAddress(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public void addAddress() { try { pageIndex = (int) raf.length() / byteLen; currPos = pageIndex * byteLen; System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "add", currPos, pageIndex + 1)); raf.seek(currPos); writeAddress(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private void updateAddress() { // TODO Auto-generated method stub try { if (raf.getFilePointer() != 0) { raf.seek(raf.getFilePointer() - byteLen); } System.out.println(String.format("当前操作:%s 指针位置:%d 当前页数:%d", "update", currPos, pageIndex + 1)); writeAddress(); } catch (IOException e) { e.printStackTrace(); } } public void writeAddress() { try { raf.writeUTF(String.format("%-32s", tfname.getText()).substring(0, 32));//有-是左对齐,在右边补空格,没有-是右对齐,在左边补空格 raf.writeUTF(String.format("%-32s", tfstreet.getText()).substring(0, 32)); raf.writeUTF(String.format("%-20s", tfcity.getText()).substring(0, 20)); raf.writeUTF(String.format("%-2s", tfstate.getText()).substring(0, 2)); raf.writeUTF(String.format("%-5s", tfzip.getText()).substring(0, 5)); } catch (IOException e) { e.printStackTrace(); } } public void readAddress() { try { tfname.setText(raf.readUTF().trim()); tfstreet.setText(raf.readUTF().trim()); tfcity.setText(raf.readUTF().trim()); tfstate.setText(raf.readUTF().trim()); tfzip.setText(raf.readUTF().trim()); } catch (IOException e) { e.printStackTrace(); } } }