簡體   English   中英

寫入和讀取二進制文件

[英]Write and Read binary file

我有一個測試:將 50000 到 60000 之間的數字寫入任何格式的文件,然后將此數據讀取到原始數字(文件大小限制 20kb )並且不能使用 writeShort() 方法寫入或讀取 但我無法將文件讀取到原始編號,我的代碼如下:

這是寫

 DataOutputStream out = new DataOutputStream(new FileOutputStream("D:\\mydata2.txt"));
            for (int i = 50000; i <= 60000; i++) {
                out.write(i);//write() just write 8-bit
            }
            out.close();

這是讀

DataInputStream in = new DataInputStream(new FileInputStream("D:\\mydata2.txt"));
    int i=0;
    while (in.available() > 0) {
        System.out.print(in.readUnsignedByte()+" ");
    }
    in.close();

Output 像這樣:

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 182 183 184 185 186 186 187 188 188 189 190 191 192 192 192 193 193 193 193 199 196 199 199 199 199 199 199 19 199 200 202 202 202 202 206 206 207 207 207 209 209 209 209 210 212 212 212 212 213 214 214 215 216 216 217 218 218 218 218 230 231 232 233 234 235 236 237 238 239 239 240 241 242 242 243 244 245 246 246 247 248 248 249 251 252 253 253 253 254 254 254 254 254 254 255 0 1 2 3 4 5 6 7 8 9 10 11 12 14 15 15 15 15 16 17 17 17 19 19 19 20 20 21 22 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 10 6 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 1817 8 918 181

這是“困難部分”的線索。 您必須執行 rest:

byte[] buf = new byte[2];
// (In loop)
buf[0] = (byte)(n >> 8);
buf[1] = (byte)(n & 0xFF);

這是正確的方法:

你不寫短,但你寫它的字節。 同樣的邏輯也適用於閱讀。

DataOutputStream out = new DataOutputStream(new FileOutputStream("C:\\New\\mydata2.txt"));

for (int i = 50000; i <= 60000; i++)
{
    ByteBuffer dbuf = ByteBuffer.allocate(2);
    dbuf.putShort((short) i);
    byte[] bytes = dbuf.array();
    out.writeByte(bytes[0]);
    out.writeByte(bytes[1]);
}
out.flush();
out.close();

DataInputStream in = new DataInputStream(new FileInputStream("C:\\New\\mydata2.txt"));
byte[] buf = new byte[2];

while (in.available() > 0)
{
    buf[0] = in.readByte();
    buf[1] = in.readByte();

    // Since Short keyword cannot be used, we could use the following way to get the numbers:
    int num = (0xff & buf[0]) << 8  |
                    (0xff & buf[1]);

     System.out.print(num +" ");
}
in.close();

檢查以了解使用shift運算符的最后一次轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM