Reading the first 10 bytes of a file in Java

后端 未结 2 1209
挽巷
挽巷 2021-01-22 09:32
    for (String name : filenames) {   
FileInputStream in = new FileInputStream(input.readUTF());   
    int byteCounter = 0;   
    int rowCounter = 0;   
    long buff         


        
相关标签:
2条回答
  • 2021-01-22 10:14

    the problem is here

    System.out.print(Integer.toHexString(read) + "\t");
    

    read contains the number of bytes effectively read from stream. Teh bytes are in array b.

    By the way: your array has a size of 8. you should increas it to 10 if you want to read 10 bytes!

    0 讨论(0)
  • 2021-01-22 10:38
    byte[] b = new byte[10];
    new DataInputStream(new FileInputStream(input.readUTF())).readFully(b);
    

    This is the simplest way, but it throws in case there are less than 10 bytes available. In case you don't want it, use a loop. Somehow I don't get what you're doing, it really doesn't look like reading the first 10 bytes: if (byteCounter != 1000)

    0 讨论(0)
提交回复
热议问题