Android: almacena entradas en el archivo

Estoy recuperando un feed XML de una url y luego analizándolo. Lo que tengo que hacer también es almacenar que internamente al teléfono para que cuando no hay conexión a Internet puede analizar la opción guardada en lugar de la viva.

El problema que estoy enfrentando es que puedo crear el objeto url, use getInputStream para obtener el contenido, pero no me deja guardarlo.

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(); 

Cualquier idea de cómo puedo guardar el flujo de entrada para poder cargarlo más tarde.

Aclamaciones

Aquí está, la entrada es su inputStreamReader . A continuación, utilice el mismo archivo (nombre) y FileInputStream para leer los datos en el futuro.

 try { File file = new File(getCacheDir(), "cacheFileAppeal.srl"); OutputStream output = new FileOutputStream(file); try { try { 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 { output.close(); } } catch (Exception e) { e.printStackTrace(); // handle exception, define IOException and others } } finally { input.close(); } 

Función Simple

Pruebe esta sencilla función para envolverla en:

 // 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's 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(); } } } 

Gracias a Jordan LaPrise y su respuesta .

Una versión más corta:

 OutputStream out = new FileOutputStream(file); fos.write(IOUtils.read(in)); out.close(); in.close(); 
  • Android Coverflow problema en 4.0.3 Samsung Galaxy S2
  • abrir y reproducir un flujo de transporte en Android (mpeg2-ts)
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.