Ich werde Eingangsstrom von Drittanbieter-Bibliothek zu meiner Anwendung erhalten. Ich muss diesen Eingabestrom in eine Datei schreiben.Effiziente Möglichkeit, InputStream in eine Datei in Java 6 zu schreiben 6
Nach dem Code-Snippet ich versuchte:
private void writeDataToFile(Stub stub) {
OutputStream os = null;
InputStream inputStream = null;
try {
inputStream = stub.getStream();
os = new FileOutputStream("test.txt");
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
} catch (Exception e) {
log("Error while fetching data", e);
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log("Error while closing input stream", e);
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
log("Error while closing output stream", e);
}
}
}
}
Gibt es einen besseren Ansatz, dies zu tun?
Dateien und Pfade ist das Konzept von Java 7. Aber meine Anwendung läuft mit Java 6. Gibt es eine Lösung wie oben in Java 6 verfügbar ist. – Awesome
Aw shucks. OK, dann können Sie eine externe Bibliothek verwenden? Ich dachte über Guava hier – fge
InputStream out? – Puce