Android - сохранить входной поток в файл
Я извлекаю XML-канал из URL-адреса и затем разбираю его. То, что мне нужно сделать, это также хранить это внутри телефона, чтобы при отсутствии подключения к Интернету он мог анализировать сохраненную опцию, а не живую.
Проблема, с которой я сталкиваюсь, заключается в том, что я могу создать объект url, использовать getInputStream для получения содержимого, но он не позволит мне его сохранить.
URL url = null;
InputStream inputStreamReader = null;
XmlPullParser xpp = null;
url = new URL("http://*********");
inputStreamReader = getInputStream(url);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFileAppeal.srl"));
//--------------------------------------------------------
//This line is where it is erroring.
//--------------------------------------------------------
out.writeObject( inputStreamReader );
//--------------------------------------------------------
out.close();
Любые идеи о том, как я могу сэкономить поток ввода, поэтому я могу загрузить его позже.
Приветствия
Ответы
Ответ 1
Вот оно, ваш вклад inputStream
. Затем используйте тот же файл (имя) и FileInputStream
для чтения данных в будущем.
try {
File file = new File(getCacheDir(), "cacheFileAppeal.srl");
try (OutputStream output = new FileOutputStream(file)) {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
}
} finally {
input.close();
}
Ответ 2
Простая функция
Попробуйте эту простую функцию, чтобы аккуратно ее обернуть:
// Copy an InputStream to a File.
//
private void copyInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Ensure that the InputStreams are closed even if there an exception.
try {
if ( out != null ) {
out.close();
}
// If you want to close the "in" InputStream yourself then remove this
// from here but ensure that you close it yourself eventually.
in.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
Благодаря Jordan LaPrise и его ответ .
Ответ 3
Версия Kotlin (протестирована, библиотека не требуется):
fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
inputStream.use { input ->
val outputStream = FileOutputStream(outputFile)
outputStream.use { output ->
val buffer = ByteArray(4 * 1024) // buffer size
while (true) {
val byteCount = input.read(buffer)
if (byteCount < 0) break
output.write(buffer, 0, byteCount)
}
output.flush()
}
}
}
Мы используем функцию use
, которая автоматически закрывает оба потока в конце.
Потоки закрыты правильно даже в случае возникновения исключения.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html
https://kotlinlang.org/docs/tutorials/kotlin-for-py/scoped-resource-usage.html
Ответ 4
Более короткая версия:
OutputStream out = new FileOutputStream(file);
fos.write(IOUtils.read(in));
out.close();
in.close();
Ответ 5
Вот решение, которое обрабатывает все Исключения и основывается на предыдущих ответах:
void writeStreamToFile(InputStream input, File file) {
try {
try (OutputStream output = new FileOutputStream(file)) {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Ответ 6
Там путь IOUtils:
copy(InputStream input, OutputStream output)
Его код похож на:
public static long copyStream(InputStream input, OutputStream output) throws IOException {
long count = 0L;
byte[] buffer = new byte[4096];
for (int n; -1 != (n = input.read(buffer)); count += (long) n)
output.write(buffer, 0, n);
return count;
}