Internal storage

Writing

try
{
    OutputStreamWriter fout=
        new OutputStreamWriter(
            openFileOutput("prueba.txt", Context.MODE_PRIVATE));
 
    fout.write("Texto de prueba.");
    fout.close();
}
catch (Exception ex)
{
    Log.e("Ficheros", "Error al escribir fichero a memoria interna");
}

Reading

try
{
    BufferedReader fin =
        new BufferedReader(
            new InputStreamReader(
                openFileInput("prueba.txt")));
 
    String texto = fin.readLine();
    fin.close();
}
catch (Exception ex)
{
    Log.e("Ficheros", "Error al leer fichero desde memoria interna");
}

External Storage

Status SD Available

boolean sdDisponible = false;
boolean sdAccesoEscritura = false;
 
//Comprobamos el estado de la memoria externa (tarjeta SD)
String estado = Environment.getExternalStorageState();
 
if (estado.equals(Environment.MEDIA_MOUNTED))
{
    sdDisponible = true;
    sdAccesoEscritura = true;
}
else if (estado.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
    sdDisponible = true;
    sdAccesoEscritura = false;
}
else
{
    sdDisponible = false;
    sdAccesoEscritura = false;
}

Writing

try
{
    File ruta_sd = Environment.getExternalStorageDirectory();
 
    File f = new File(ruta_sd.getAbsolutePath(), "prueba_sd.txt");
 
    OutputStreamWriter fout =
        new OutputStreamWriter(
            new FileOutputStream(f));
 
    fout.write("Texto de prueba.");
    fout.close();
}
catch (Exception ex)
{
    Log.e("Ficheros", "Error al escribir fichero a tarjeta SD");
}

Reading

try
{
    File ruta_sd = Environment.getExternalStorageDirectory();
 
    File f = new File(ruta_sd.getAbsolutePath(), "prueba_sd.txt");
 
    BufferedReader fin =
        new BufferedReader(
            new InputStreamReader(
                new FileInputStream(f)));
 
    String texto = fin.readLine();
    fin.close();
}
catch (Exception ex)
{
    Log.e("Ficheros", "Error al leer fichero desde tarjeta SD");
}

Storage

By NEIFER GARCIA

Storage

  • 135