piffy icon

Java IO e Random Access Binario

piffy | PRO | 01/01/18 01:27:29 PM UTC | 0 ⭐ | 749 👁️ | Never ⏰ | []
Java |

900 B

|

None

|

0 👍

/

0 👎

import java.io.IOException;
import java.io.RandomAccessFile;
 
 
public class RandomFile {
 
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
        raf.write("ABCDEFGHIJKLMNOPQRSTUWXYZ".getBytes());
        raf.close();
        raf = new RandomAccessFile("file.txt", "rw");
        raf.seek(raf.length());
        raf.write("Aggiunta".getBytes());
        raf.close();;
        byte[] bytes = new byte[28];
        raf = new RandomAccessFile("file.txt", "r");
        System.out.println("Byte letti: " + raf.read(bytes));
        for (byte by:bytes)
        System.out.println((char) by);
        raf.seek(4);
        bytes = new byte[1];
        System.out.println("Byte letti: " + raf.read(bytes));
        for (byte by:bytes)
            System.out.println((char) by);
        raf.close();
 
 
    }
}

Comments