piffy

Java IO e Random Access Binario

Jan 1st, 2018
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3.  
  4.  
  5. public class RandomFile {
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.         RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
  9.         raf.write("ABCDEFGHIJKLMNOPQRSTUWXYZ".getBytes());
  10.         raf.close();
  11.         raf = new RandomAccessFile("file.txt", "rw");
  12.         raf.seek(raf.length());
  13.         raf.write("Aggiunta".getBytes());
  14.         raf.close();;
  15.         byte[] bytes = new byte[28];
  16.         raf = new RandomAccessFile("file.txt", "r");
  17.         System.out.println("Byte letti: " + raf.read(bytes));
  18.         for (byte by:bytes)
  19.         System.out.println((char) by);
  20.         raf.seek(4);
  21.         bytes = new byte[1];
  22.         System.out.println("Byte letti: " + raf.read(bytes));
  23.         for (byte by:bytes)
  24.             System.out.println((char) by);
  25.         raf.close();
  26.  
  27.  
  28.     }
  29. }
RAW Paste Data