This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
java:byte_streams_to_data [2020/03/04 14:38] gthanos |
java:byte_streams_to_data [2021/04/04 15:44] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Μετασχηματισμός των ροών δεδομένων σε τύπους δεδομένων ====== | ||
| - | |||
| - | Ο προηγούμενος κώδικας δουλεύει εξαιρετικά με ροές από bytes, στην πραγματικότητα όμως τα δεδομένα που θέλουμε να αποθηκεύσουμε ή να διαβάσουμε μπορεί να είναι ακέραιοι, | ||
| - | |||
| - | Για παράδειγμα, | ||
| - | - να δημιουργήσουμε ένα αντικείμενο της κλάσης [[https:// | ||
| - | - να αποθηκεύσουμε τις επιμέρους πληροφορίες στο buffer με τη σειρά που θέλουμε να αποθηκευτούν. | ||
| - | - να εξάγουμε από το buffer ένα πίνακα από bytes που περιέχει τη σχετική πληροφορία και τον πίνακα αυτό να τον γράψουμε στο [[https:// | ||
| - | |||
| - | Η διαδικασία ανάγνωσης είναι ακριβώς η αντίστροφή, | ||
| - | - διαβάζουμε από ένα [[https:// | ||
| - | - Από τον πίνακα αυτό δημιουργούμε ένα [[https:// | ||
| - | - Από το [[https:// | ||
| - | |||
| - | Το παρακάτω πρόγραμμα καλεί αρχικά τη συνάρτηση write η οποία αποθηκεύει έναν ακέραιο, | ||
| - | |||
| - | <code java BufferReadWriteTest.java> | ||
| - | import java.nio.*; | ||
| - | import java.io.*; | ||
| - | |||
| - | public class ByteBufferReadWriteTest { | ||
| - | | ||
| - | /* this method is only for debugging. | ||
| - | * Comment out code, where method is called. | ||
| - | */ | ||
| - | public static void print_array(byte[] array) { | ||
| - | int i=0; | ||
| - | for(byte b: array) { | ||
| - | System.out.format(" | ||
| - | if(++i % 2 == 0) | ||
| - | System.out.print(" | ||
| - | } | ||
| - | System.out.println(); | ||
| - | } | ||
| - | | ||
| - | public static void main(String []args ) { | ||
| - | File file = new File(" | ||
| - | write(file); | ||
| - | read(file); | ||
| - | } | ||
| - | | ||
| - | public static void write(File file) { | ||
| - | int a = -159954; | ||
| - | double b = 125.128953; | ||
| - | char c = ' | ||
| - | String str = " | ||
| - | | ||
| - | ByteBuffer buffer = ByteBuffer.allocate(34); | ||
| - | buffer.putInt(a); | ||
| - | buffer.putDouble(b); | ||
| - | buffer.putChar(c); | ||
| - | buffer.put(str.getBytes(java.nio.charset.StandardCharsets.UTF_8)); | ||
| - | | ||
| - | try(FileOutputStream out = new FileOutputStream(file)) { | ||
| - | byte []array = buffer.array(); | ||
| - | out.write(array); | ||
| - | // just for debugging purposes | ||
| - | // print_array(array); | ||
| - | } | ||
| - | catch(IOException ex) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | | ||
| - | public static void read(File file) { | ||
| - | int a; | ||
| - | double b; | ||
| - | double c; | ||
| - | | ||
| - | byte array[] = new byte[512]; | ||
| - | | ||
| - | try(FileInputStream in = new FileInputStream(file)) { | ||
| - | in.read(array); | ||
| - | } | ||
| - | catch(IOException ex) { | ||
| - | | ||
| - | } | ||
| - | // just for debugging purposes | ||
| - | // print_array(array); | ||
| - | | ||
| - | ByteBuffer buffer = ByteBuffer.wrap(array); | ||
| - | System.out.println(buffer.getInt()); | ||
| - | System.out.println(buffer.getDouble()); | ||
| - | System.out.println(buffer.getChar()); | ||
| - | byte [] str = new byte[18]; | ||
| - | buffer.get(str); | ||
| - | System.out.println(new String(str, java.nio.charset.StandardCharsets.UTF_8)); | ||
| - | | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP tip 80% center round> | ||
| - | H //default// υλοποίηση ενός [[https:// | ||
| - | </ | ||