Wie konvertiere ich einen Java.io.File
in einen byte[]
?
Es hängt davon ab, was für Sie am besten ist. Was die Produktivität angeht, erfinden Sie das Rad nicht neu und verwenden Sie Apache Commons. Welches ist hier IOUtils.toByteArray(InputStream input)
.
Ab JDK 7 können Sie Files.readAllBytes(Path)
verwenden.
Beispiel:
import Java.io.File;
import Java.nio.file.Files;
File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
import Java.io.RandomAccessFile;
RandomAccessFile f = new RandomAccessFile(fileName, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
Dokumentation für Java 8: http://docs.Oracle.com/javase/8/docs/api/Java/io/RandomAccessFile.html
Seit JDK 7 - ein Liner:
byte[] array = Files.readAllBytes(new File("/path/to/file").toPath());
Keine externen Abhängigkeiten erforderlich.
Grundsätzlich muss man es in den Speicher lesen. Öffnen Sie die Datei, ordnen Sie das Array zu und lesen Sie den Inhalt aus der Datei in das Array.
Der einfachste Weg ist etwas Ähnliches:
public byte[] read(File file) throws IOException, FileTooBigException {
if (file.length() > MAX_FILE_SIZE) {
throw new FileTooBigException(file);
}
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1) {
ous.write(buffer, 0, read);
}
}finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
}
}
return ous.toByteArray();
}
Das Kopieren der Dateiinhalte ist unnötig (tatsächlich werden die Daten dreimal kopiert: von der Datei in buffer
, von buffer
nach ByteArrayOutputStream
von ByteArrayOutputStream
zum tatsächlich resultierenden Array).
Sie müssen auch sicherstellen, dass Sie nur Dateien bis zu einer bestimmten Größe (dies ist in der Regel von der Anwendung abhängig) in den Speicher einlesen :-).
Sie müssen das IOException
auch außerhalb der Funktion behandeln.
Ein anderer Weg ist dieser:
public byte[] read(File file) throws IOException, FileTooBigException {
if (file.length() > MAX_FILE_SIZE) {
throw new FileTooBigException(file);
}
byte[] buffer = new byte[(int) file.length()];
InputStream ios = null;
try {
ios = new FileInputStream(file);
if (ios.read(buffer) == -1) {
throw new IOException(
"EOF reached while trying to read the whole file");
}
} finally {
try {
if (ios != null)
ios.close();
} catch (IOException e) {
}
}
return buffer;
}
Dies hat kein unnötiges Kopieren zur Folge.
FileTooBigException
ist eine benutzerdefinierte Anwendungsausnahme. .__ Die Konstante MAX_FILE_SIZE
ist ein Anwendungsparameter.
Bei großen Dateien sollten Sie wahrscheinlich einen Stream-Verarbeitungsalgorithmus oder eine Speicherzuordnung (siehe Java.nio
) verwenden.
Wie jemand sagte, könnten Apache Commons File Utils das haben, wonach Sie suchen
public static byte[] readFileToByteArray(File file) throws IOException
Beispiel use (Program.Java
):
import org.Apache.commons.io.FileUtils;
public class Program {
public static void main(String[] args) throws IOException {
File file = new File(args[0]); // assume args[0] is the path to file
byte[] data = FileUtils.readFileToByteArray(file);
...
}
}
Sie können dazu auch die NIO-API verwenden. Ich könnte dies mit diesem Code tun, solange die Gesamtgröße (in Bytes) in ein int passen würde.
File f = new File("c:\\wscp.script");
FileInputStream fin = null;
FileChannel ch = null;
try {
fin = new FileInputStream(f);
ch = fin.getChannel();
int size = (int) ch.size();
MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
byte[] bytes = new byte[size];
buf.get(bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fin != null) {
fin.close();
}
if (ch != null) {
ch.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Ich denke, es ist sehr schnell, da es MappedByteBuffer verwendet.
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
throw new IOException("File is too large!");
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
InputStream is = new FileInputStream(file);
try {
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
} finally {
is.close();
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
return bytes;
}
Wenn Sie nicht über Java 8 verfügen und mit mir übereinstimmen, ist die Verwendung einer umfangreichen Bibliothek, die das Schreiben einiger Zeilen Code vermeidet, eine schlechte Idee:
public static byte[] readBytes(InputStream inputStream) throws IOException {
byte[] b = new byte[1024];
ByteArrayOutputStream os = new ByteArrayOutputStream();
int c;
while ((c = inputStream.read(b)) != -1) {
os.write(b, 0, c);
}
return os.toByteArray();
}
Der Anrufer ist für das Schließen des Streams verantwortlich.
Einfacher Weg:
File fff = new File("/path/to/file");
FileInputStream fileInputStream = new FileInputStream(fff);
// int byteLength = fff.length();
// In Android the result of file.length() is long
long byteLength = fff.length(); // byte count of the file-content
byte[] filecontent = new byte[(int) byteLength];
fileInputStream.read(filecontent, 0, (int) byteLength);
Einfachste Möglichkeit zum Lesen von Bytes aus der Datei
import Java.io.*;
class ReadBytesFromFile {
public static void main(String args[]) throws Exception {
// getBytes from anyWhere
// I'm getting byte array from File
File file = null;
FileInputStream fileStream = new FileInputStream(file = new File("ByteArrayInputStreamClass.Java"));
// Instantiate array
byte[] arr = new byte[(int) file.length()];
// read All bytes of File stream
fileStream.read(arr, 0, arr.length);
for (int X : arr) {
System.out.print((char) X);
}
}
}
Guava hat Files.toByteArray () zu bieten. Das hat mehrere Vorteile:
Verwenden Sie den gleichen Ansatz wie die Community-Wiki-Antwort, aber sauberer und kompiliert (wenn Sie nicht Apache Commons-Bibliotheken importieren möchten, z. B. auf Android):
public static byte[] getFileBytes(File file) throws IOException {
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1)
ous.write(buffer, 0, read);
} finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
// swallow, since not that important
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
// swallow, since not that important
}
}
return ous.toByteArray();
}
import Java.io.File;
import Java.nio.file.Files;
import Java.nio.file.Path;
File file = getYourFile();
Path path = file.toPath();
byte[] data = Files.readAllBytes(path);
Ich glaube das ist der einfachste Weg:
org.Apache.commons.io.FileUtils.readFileToByteArray(file);
ReadFully Liest b.length Bytes aus dieser Datei in das Bytearray, beginnend am aktuellen Dateizeiger. Diese Methode liest wiederholt aus der Datei, bis die angeforderte Anzahl von Bytes gelesen ist. Diese Methode blockiert, bis die angeforderte Anzahl von Bytes gelesen wird, das Ende des Streams erkannt wird oder eine Ausnahme ausgelöst wird.
RandomAccessFile f = new RandomAccessFile(fileName, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
Wenn Sie Bytes in einen vorab zugewiesenen Bytepuffer einlesen möchten, kann diese Antwort hilfreich sein.
Ihre erste Vermutung wäre wahrscheinlich die Verwendung von InputStream read(byte[])
. Diese Methode hat jedoch einen Fehler, der die Verwendung unverhältnismäßig schwierig macht: Es gibt keine Garantie, dass das Array tatsächlich vollständig gefüllt wird, auch wenn kein EOF gefunden wird.
Betrachten Sie stattdessen DataInputStream readFully(byte[])
. Dies ist ein Wrapper für Eingabeströme und hat nicht das oben genannte Problem. Außerdem wird diese Methode ausgelöst, wenn EOF angetroffen wird. Viel schöner.
Lassen Sie mich eine weitere Lösung hinzufügen, ohne Bibliotheken von Drittanbietern zu verwenden. Es verwendet ein Ausnahmebehandlungsmuster, das von Scott ( link ) vorgeschlagen wurde. Und ich habe den hässlichen Teil in eine separate Nachricht verschoben (ich würde mich in einer FileUtils-Klasse verstecken;)
public void someMethod() {
final byte[] buffer = read(new File("test.txt"));
}
private byte[] read(final File file) {
if (file.isDirectory())
throw new RuntimeException("Unsupported operation, file "
+ file.getAbsolutePath() + " is a directory");
if (file.length() > Integer.MAX_VALUE)
throw new RuntimeException("Unsupported operation, file "
+ file.getAbsolutePath() + " is too big");
Throwable pending = null;
FileInputStream in = null;
final byte buffer[] = new byte[(int) file.length()];
try {
in = new FileInputStream(file);
in.read(buffer);
} catch (Exception e) {
pending = new RuntimeException("Exception occured on reading file "
+ file.getAbsolutePath(), e);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
if (pending == null) {
pending = new RuntimeException(
"Exception occured on closing file"
+ file.getAbsolutePath(), e);
}
}
}
if (pending != null) {
throw new RuntimeException(pending);
}
}
return buffer;
}
public static byte[] readBytes(InputStream inputStream) throws IOException {
byte[] buffer = new byte[32 * 1024];
int bufferSize = 0;
for (;;) {
int read = inputStream.read(buffer, bufferSize, buffer.length - bufferSize);
if (read == -1) {
return Arrays.copyOf(buffer, bufferSize);
}
bufferSize += read;
if (bufferSize == buffer.length) {
buffer = Arrays.copyOf(buffer, bufferSize * 2);
}
}
}
Eine andere Möglichkeit zum Lesen von Bytes aus der Datei
Reader reader = null;
try {
reader = new FileReader(file);
char buf[] = new char[8192];
int len;
StringBuilder s = new StringBuilder();
while ((len = reader.read(buf)) >= 0) {
s.append(buf, 0, len);
byte[] byteArray = s.toString().getBytes();
}
} catch(FileNotFoundException ex) {
} catch(IOException e) {
}
finally {
if (reader != null) {
reader.close();
}
}
Versuche dies :
try
{
String path="";
InputStream inputStream=new FileInputStream(path);
byte[] data=IOUtils.readFully(inputStream,-1,false);
}
catch (Exception e)
{
}
Vergessen Sie nicht, "import Sun.misc.IOUtils;"
Auf folgende Weise wird eine Java.io.File nicht nur in ein Byte [] konvertiert. Ich habe auch festgestellt, dass dies der schnellste Weg ist, eine Datei zu lesen, wenn viele verschiedene Java-Dateilese-Methoden gegeneinander getestet werden:
Java.nio.file.Files.readAllBytes ()
import Java.io.File;
import Java.io.IOException;
import Java.nio.file.Files;
public class ReadFile_Files_ReadAllBytes {
public static void main(String [] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-10KB.txt";
File file = new File(fileName);
byte [] fileBytes = Files.readAllBytes(file.toPath());
char singleChar;
for(byte b : fileBytes) {
singleChar = (char) b;
System.out.print(singleChar);
}
}
}