add file open/save functions.

This commit is contained in:
ehouse 2010-01-20 06:35:56 +00:00
parent 406a6a4a98
commit 531ae8ea1a

View file

@ -7,6 +7,8 @@ import java.lang.Thread;
import java.text.MessageFormat;
import android.widget.Toast;
import android.content.Context;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class Utils {
static final String TAG = "EJAVA";
@ -35,4 +37,37 @@ public class Utils {
Toast.makeText( context, text, Toast.LENGTH_LONG ).show();
}
public static byte[] savedGame( Context context, String path )
{
byte[] stream = null;
try {
FileInputStream in = context.openFileInput( path );
int len = in.available();
Utils.logf( "savedGame: got " + len + " bytes." );
stream = new byte[len];
in.read( stream, 0, len );
in.close();
} catch ( java.io.FileNotFoundException fnf ) {
Utils.logf( fnf.toString() );
stream = null;
} catch ( java.io.IOException io ) {
Utils.logf( io.toString() );
stream = null;
}
return stream;
} // savedGame
public static void saveGame( Context context, byte[] bytes, String path )
{
try {
FileOutputStream out = context.openFileOutput( path, Context.MODE_PRIVATE );
out.write( bytes );
out.close();
} catch ( java.io.IOException ex ) {
Utils.logf( "got IOException: " + ex.toString() );
}
}
}