如何在Java中读取文件字节?

时间:2011-09-07 03:14:02

标签: java arrays

如何从文件读取字节,并将其放入字节数组而不将字节转换为整数?

2 个答案:

答案 0 :(得分:1)

FileInputStream fis = new FileInputStream("your file name");
byte[] bytes = new byte[100]; // replace 100 with the desired size, of course
int offset = 0;               // which element to stuff the byte into  

fis.read(bytes, offset, 1);   // the 1 is how many bytes to read

答案 1 :(得分:0)

    byte ch;
    try {
      is = new DataInputStream(new FileInputStream("fileName.dat"));
      while (true) { 
        ch = is.readByte();
   // put here in any byte array ...
        System.out.flush();
      }
    }