Search This Blog

28 May 2010

Writing files

Let's start by creating a basic app:

F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml

Choose the galatea-archetype plugin:
groupId: org.eoti.android
artifactId: FileTest
version: 1.0-SNAPSHOT
package: org.eoti.android


F:\work> cd FileTest
F:\work\FileTest> mvn clean install

Let's rewrite our hello world activity:
public class FileTestActivity extends Activity {
    private static String TAG = "FileTest";
    private static String FILENAME_PRIVATE = "filetest1.txt";
    private static String FILENAME_SDCARD = "/sdcard/filetest2.txt";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            PrintWriter out = new PrintWriter(openFileOutput(FILENAME_PRIVATE, MODE_PRIVATE));
            out.format("This is a test!\n");
            out.close();
            Log.i(TAG, "try: adb pull /data/data/org.eoti.android/files/filetest1.txt");
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Error writing file #1", e);
        }

        try{
            PrintWriter out = new PrintWriter(new File(FILENAME_SDCARD));
            out.format("This is another test!\n");
            out.close();
            Log.i(TAG, "try: adb pull /sdcard/filetest2.txt");
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Error writing file #2", e);
        }
    }
}

Redeploy (mvn clean install) and run the app.  You should now be able to grab the generated files from another command prompt:
adb pull /data/data/org.eoti.android/files/filetest1.txt
adb pull /sdcard/filetest2.txt

No comments:

Post a Comment