You are on page 1of 2

AndroidManifest.

xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Save and Load for MainActivity.java:

public static void Save(File file, String[] data)


{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
try
{
for (int i = 0; i<data.length; i++)
{
fos.write(data[i].getBytes());
if (i < data.length-1)
{
fos.write("\n".getBytes());
}
}
}
catch (IOException e) {e.printStackTrace();}
}
finally
{
try
{
fos.close();
}
catch (IOException e) {e.printStackTrace();}
}
}

public static String[] Load(File file)


{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

String test;
int anzahl=0;
try
{
while ((test=br.readLine()) != null)
{
anzahl++;
}
}
catch (IOException e) {e.printStackTrace();}

try
{
fis.getChannel().position(0);
}
catch (IOException e) {e.printStackTrace();}

String[] array = new String[anzahl];

String line;
int i = 0;
try
{
while((line=br.readLine())!=null)
{
array[i] = line;
i++;
}
}
catch (IOException e) {e.printStackTrace();}
return array;
}

You might also like