Comprensión integral de las matrices de bytes de Java y lectura y escritura de archivos
import java.io.bytearRaRyOutputStream; import java.io.file; import java.io.fileInputStream; import java.io.fileOutputStream; import java.io.ioException; public class FileHelper {<span style = "White-space: pre"> </span> // La primera forma de obtener el contenido de archivo público byte [] GetContent (spil style) IoException {archivo archivo = nuevo archivo (filepath); Long filesize = file.length (); if (filesize> integer.max_value) {system.out.println ("archivo demasiado grande ..."); regresar nulo; } FileInputStream fi = new FileInputStream (archivo); byte [] 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; } // Asegúrese de que todos los datos se lean si (offset! = Buffer.length) {Throw New IOException ("no se pudo leer completamente el archivo" + file.getName ()); } fi.close (); buffer de retorno; } // La segunda forma de obtener File Content public Byte [] getContent2 (String FilePath) lanza IOException {fileInputStream in = newInputStream (FilePath); ByteArRayOutputStream out = new ByteArRayOutputStream (1024); System.out.println ("bytes disponibles:"+in.available ()); byte [] temp = new Byte [1024]; int tamaño = 0; while ((size = in.read (temp))! =-1) {out.write (temp, 0, size); } in.close (); byte [] bytes = out.tobytearray (); System.out.println ("bytes size got es:"+bytes.length); bytes de devolución; } // Escribe la matriz de byte en el archivo public void createFile (string ruta, byte [] content) lanza ioexception {fileOutputStream fos = new FileOutputStream (ruta); fos.write (contenido); fos.close (); }}El artículo anterior comprende de manera integral las matrices de bytes Java y la lectura y la escritura de archivos es todo el contenido que comparto con usted. Espero que pueda darle una referencia y espero que pueda apoyar más a Wulin.com.