public static byte[] getFileByteArray(File file) { long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { System.out.println("file too big..."); return null; } byte[] buffer = null; try (FileInputStream fi = new FileInputStream(file)) { buffer = new byte[(int) fileSize]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } // 确保所有数据均被读取 if (offset != buffer.length) { throw new IOException("Could not completely read file " + file.getName()); } } catch (Exception e) { e.printStackTrace(); } return buffer; }
原文地址:https://www.cnblogs.com/toSeeMyDream/p/12381183.html
时间: 2024-10-08 16:37:11