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/09 05:17] gthanos [DataInputStream & DataOutputStream] |
java:byte_streams_to_data [2021/04/04 15:44] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Μετασχηματισμός των ροών δυαδικών δεδομένων σε βασικούς τύπους δεδομένων ====== | ||
| - | |||
| - | Ο προηγούμενος κώδικας δουλεύει εξαιρετικά με ροές από bytes, στην πραγματικότητα όμως τα δεδομένα που θέλουμε να αποθηκεύσουμε ή να διαβάσουμε μπορεί να είναι ακέραιοι, | ||
| - | |||
| - | Για παράδειγμα, | ||
| - | - να δημιουργήσουμε ένα αντικείμενο της κλάσης [[https:// | ||
| - | - να αποθηκεύσουμε τις επιμέρους πληροφορίες στο buffer με τη σειρά που θέλουμε να αποθηκευτούν. | ||
| - | - να εξάγουμε από το buffer ένα πίνακα από bytes που περιέχει τη σχετική πληροφορία και τον πίνακα αυτό να τον γράψουμε στο [[https:// | ||
| - | |||
| - | Η διαδικασία ανάγνωσης είναι ακριβώς η αντίστροφή, | ||
| - | - διαβάζουμε από ένα [[https:// | ||
| - | - από τον πίνακα αυτό δημιουργούμε ένα [[https:// | ||
| - | - από το [[https:// | ||
| - | |||
| - | Το παρακάτω πρόγραμμα καλεί αρχικά τη συνάρτηση '' | ||
| - | |||
| - | <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:// | ||
| - | Περισσότερες πληροφορίες για τις διαφορές μεταξύ big-endian και little-endian μπορείτε να βρείτε [[https:// | ||
| - | </ | ||
| - | |||
| - | <WRAP todo 80% center round> | ||
| - | - Αλλάξτε το παραπάνω παράδειγμα, | ||
| - | - Βάλτε σε σχόλια τη στατική μέθοδο '' | ||
| - | </ | ||
| - | |||
| - | |||
| - | ====== DataInputStream & DataOutputStream ====== | ||
| - | |||
| - | Στις περιπτώσεις που τα δεδομένα γνωρίζουμε ότι έχουν αποθηκευτεί κατά big-endian εναλλακτικά της χρήσης των παραπάνω κλάσεων μπορούμε να χρησιμοποιήσουμε τις απλούστερες κλάσεις [[https:// | ||
| - | |||
| - | Παρακάτω παρατίθεται το παραπάνω πρόγραμμα αλλαγμένο ώστε να χρησιμοποιεί τα [[https:// | ||
| - | |||
| - | <code java DataInputOutputStreamTest.java> | ||
| - | |||
| - | import java.nio.*; | ||
| - | import java.io.*; | ||
| - | |||
| - | public class DataInputOutputStreamTest { | ||
| - | | ||
| - | /* 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 = " | ||
| - | | ||
| - | try(DataOutputStream out = new DataOutputStream(new FileOutputStream(file))) { | ||
| - | out.writeInt(a); | ||
| - | out.writeDouble(b); | ||
| - | out.writeChar(c); | ||
| - | byte [] bytes_str = str.getBytes(java.nio.charset.StandardCharsets.UTF_16); | ||
| - | out.write(bytes_str, | ||
| - | } | ||
| - | catch(IOException ex) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | | ||
| - | public static void read(File file) { | ||
| - | | ||
| - | try(DataInputStream in = new DataInputStream(new FileInputStream(file))) { | ||
| - | int a = in.readInt(); | ||
| - | double b = in.readDouble(); | ||
| - | char c = in.readChar(); | ||
| - | byte [] str = new byte[18]; | ||
| - | in.read(str); | ||
| - | ByteBuffer buffer = ByteBuffer.wrap(str); | ||
| - | System.out.println(new String(str, java.nio.charset.StandardCharsets.UTF_16)); | ||
| - | System.out.println(a); | ||
| - | System.out.println(b); | ||
| - | System.out.println(c); | ||
| - | // | ||
| - | } | ||
| - | catch(IOException ex) { | ||
| - | | ||
| - | } | ||
| - | | ||
| - | } | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | <WRAP tip 80% center round> | ||
| - | Η κλάσεις [[https:// | ||
| - | |||
| - | </ | ||
| - | |||
| - | <WRAP todo 80% center round> | ||
| - | Παρατηρήστε ότι και στις δύο περιπτώσεις μπορείτε να αλλάξετε την κωδικοποίηση των Strings που αποθηκεύονται μέσα στο stream. Στα παραπάνω παραδείγματα αλλάξτε την κωδικοποίηση σε UTF-8 από UTF-16. | ||
| - | </ | ||
| - | |||
| - | |Προηγούμενο: | ||