书中第三章列出了一个游戏开发的基本框架所要实现的接口
package com.badlogic.androidgames.framework; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public interface FileIO { public InputStream readAsset(String fileName) throws IOException; public InputStream readFile(String fileName) throws IOException; public OutputStream writeFile(String fileName) throws IOException; }
这个类可以读取手机内存文件,Asset目录文件,SD卡文件,及ShardPreferences
package com.badlogic.androidgames.framework.impl; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.content.SharedPreferences; import android.content.res.AssetManager; import android.os.Environment; import android.preference.PreferenceManager; import com.badlogic.androidgames.framework.FileIO; public class AndroidFileIO implements FileIO { Context context; AssetManager assets; String externalStoragePath; public AndroidFileIO(Context context) { this.context = context; this.assets = context.getAssets(); this.externalStoragePath = Environment.getExternalStorageDirectory() .getAbsolutePath() + File.separator; } public InputStream readAsset(String fileName) throws IOException { return assets.open(fileName); } public InputStream readFile(String fileName) throws IOException { return new FileInputStream(externalStoragePath + fileName); } public OutputStream writeFile(String fileName) throws IOException { return new FileOutputStream(externalStoragePath + fileName); } public SharedPreferences getPreferences() { return PreferenceManager.getDefaultSharedPreferences(context); } }