import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; class Demo { // Helper function private static byte[] readChunk(DataInputStream ds, int len) throws IOException { byte[] buf = new byte[len]; ds.read(buf, 0, buf.length); return buf; } static void readNetwork(String absFile) throws IOException { DataInputStream ds = new DataInputStream(new FileInputStream(absFile)); byte b = ds.readByte(); // Read 1 byte number short bu = (short)ds.readUnsignedByte(); // Read unsigned 1 byte number short s = ds.readShort(); // Read 2 byte number int su = ds.readUnsignedShort(); // Read unsigned 2 byte number int i = ds.readInt(); // Read 4 byte number int su = ds.readUnsignedInt(); // This method doesn't exist long l = ds.readLong(); // Read 8 byte number byte[] chunk = readChunk(ds, 10); // Read 10 byte buffer } public static final void main(String[] args) throws IOException { readNetwork("/tmp/myfile.dat"); } }
Programming Tips - Java: How to read in "network" order
Date: 2019mar15
Update: 2025oct8
Language: Java
Q. Java: How to read in "network" order
(Big Endian)
A. Use DataInputSteam as shown in this full example: