java:byte_streams_to_data
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:byte_streams_to_data [2020/03/09 20:57] – [Μετασχηματισμός των ροών δυαδικών δεδομένων σε βασικούς τύπους δεδομένων] gthanos | java:byte_streams_to_data [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| Το παρακάτω πρόγραμμα καλεί αρχικά τη συνάρτηση '' | Το παρακάτω πρόγραμμα καλεί αρχικά τη συνάρτηση '' | ||
| - | <code java BufferReadWriteTest.java> | + | <code java ByteBufferReadWriteTest.java> |
| import java.nio.*; | import java.nio.*; | ||
| import java.io.*; | import java.io.*; | ||
| + | import java.util.Arrays; | ||
| public class ByteBufferReadWriteTest { | public class ByteBufferReadWriteTest { | ||
| Line 35: | Line 36: | ||
| | | ||
| public static void main(String []args ) { | public static void main(String []args ) { | ||
| - | File file = new File("my.bin" | + | File file = new File("file.bin" |
| write(file); | write(file); | ||
| read(file); | read(file); | ||
| Line 43: | Line 44: | ||
| int a = -159954; | int a = -159954; | ||
| double b = 125.128953; | double b = 125.128953; | ||
| - | char c = 'Θ'; | + | char c = 'θ'; |
| String str = " | String str = " | ||
| | | ||
| - | ByteBuffer buffer = ByteBuffer.allocate(34); | + | ByteBuffer buffer = ByteBuffer.allocate(512); |
| + | buffer.order(ByteOrder.BIG_ENDIAN); | ||
| buffer.putInt(a); | buffer.putInt(a); | ||
| buffer.putDouble(b); | buffer.putDouble(b); | ||
| buffer.putChar(c); | buffer.putChar(c); | ||
| buffer.put(str.getBytes(java.nio.charset.StandardCharsets.UTF_8)); | buffer.put(str.getBytes(java.nio.charset.StandardCharsets.UTF_8)); | ||
| + | int buffer_size = buffer.position(); | ||
| | | ||
| try(FileOutputStream out = new FileOutputStream(file)) { | try(FileOutputStream out = new FileOutputStream(file)) { | ||
| byte []array = buffer.array(); | byte []array = buffer.array(); | ||
| + | array = Arrays.copyOf(array, | ||
| out.write(array); | out.write(array); | ||
| // just for debugging purposes | // just for debugging purposes | ||
| - | // print_array(array); | + | // |
| } | } | ||
| catch(IOException ex) { | catch(IOException ex) { | ||
| Line 69: | Line 73: | ||
| | | ||
| byte array[] = new byte[512]; | byte array[] = new byte[512]; | ||
| - | | + | |
| try(FileInputStream in = new FileInputStream(file)) { | try(FileInputStream in = new FileInputStream(file)) { | ||
| - | in.read(array); | + | |
| + | array = Arrays.copyOf(array, | ||
| } | } | ||
| catch(IOException ex) { | catch(IOException ex) { | ||
| Line 77: | Line 82: | ||
| } | } | ||
| // just for debugging purposes | // just for debugging purposes | ||
| - | // print_array(array); | + | // |
| | | ||
| ByteBuffer buffer = ByteBuffer.wrap(array); | ByteBuffer buffer = ByteBuffer.wrap(array); | ||
| + | buffer.order(ByteOrder.BIG_ENDIAN); | ||
| System.out.println(buffer.getInt()); | System.out.println(buffer.getInt()); | ||
| System.out.println(buffer.getDouble()); | System.out.println(buffer.getDouble()); | ||
| - | System.out.println(buffer.getChar()); | + | System.out.println("'" |
| - | byte [] str = new byte[18]; | + | int str_size = buffer.remaining(); |
| - | buffer.get(str); | + | byte[] |
| - | System.out.println(new String(str, java.nio.charset.StandardCharsets.UTF_8)); | + | buffer.get(bytes); |
| - | | + | System.out.println(new String(bytes, java.nio.charset.StandardCharsets.UTF_8)); |
| } | } | ||
| } | } | ||
| Line 92: | Line 98: | ||
| <WRAP tip 80% center round> | <WRAP tip 80% center round> | ||
| - | H //default// υλοποίηση ενός [[https:// | + | H //default// υλοποίηση ενός [[https:// |
| Πληροφορίες για τη διαφορά μεταξύ big-endian και little-endian μπορείτε να βρείτε [[https:// | Πληροφορίες για τη διαφορά μεταξύ big-endian και little-endian μπορείτε να βρείτε [[https:// | ||
| </ | </ | ||
| Line 109: | Line 115: | ||
| <code java DataInputOutputStreamTest.java> | <code java DataInputOutputStreamTest.java> | ||
| - | |||
| import java.nio.*; | import java.nio.*; | ||
| import java.io.*; | import java.io.*; | ||
| Line 129: | Line 134: | ||
| | | ||
| public static void main(String []args ) { | public static void main(String []args ) { | ||
| - | File file = new File("my.bin" | + | File file = new File("file.bin" |
| write(file); | write(file); | ||
| read(file); | read(file); | ||
| Line 137: | Line 142: | ||
| int a = -159954; | int a = -159954; | ||
| double b = 125.128953; | double b = 125.128953; | ||
| - | char c = 'Ξ'; | + | char c = 'Θ'; |
| String str = " | String str = " | ||
| | | ||
| Line 158: | Line 163: | ||
| double b = in.readDouble(); | double b = in.readDouble(); | ||
| char c = in.readChar(); | char c = in.readChar(); | ||
| - | byte [] str = new byte[18]; | + | byte [] bytes = new byte[512]; |
| - | in.read(str); | + | |
| - | | + | |
| - | System.out.println(new String(str, java.nio.charset.StandardCharsets.UTF_16)); | + | |
| System.out.println(a); | System.out.println(a); | ||
| System.out.println(b); | System.out.println(b); | ||
| - | System.out.println(c); | + | System.out.println("'" |
| - | | + | System.out.println(new String(bytes, |
| } | } | ||
| catch(IOException ex) { | catch(IOException ex) { | ||
| Line 173: | Line 177: | ||
| } | } | ||
| } | } | ||
| - | |||
| </ | </ | ||
| <WRAP tip 80% center round> | <WRAP tip 80% center round> | ||
| - | Η κλάσεις [[https:// | + | Η κλάσεις [[https:// |
| </ | </ | ||
java/byte_streams_to_data.1583787427.txt.gz · Last modified: 2020/03/09 20:57 (external edit)
