-1

I've been trying to do this on my own for the past couple hours and am kinda losing it a little.

All I want to do is open a file, read it and display it to the console; that's it.

I'm using eclipse to develop for android 2.3.3.

I have tried using a bunch of different ways with code that I have found here, and on other sites. Here is what I have now and how its all called:

In the OnCreate function:

setContentView(new TestMap(this));

The testMap class:

TestMap(Context context){
        super(context);
        // might need to be on the panel class
        loadTileFile("worldonelayout.txt", context);

in the same class:

private void loadTileFile (String filename, Context context){
        FileInputStream input = null;
        InputStreamReader reader = null;

        char[] inputBuffer = new char[256];
        String data = null;

        try {
            input = context.openFileInput("worldonelayout.txt");
            reader = new InputStreamReader(input);
            reader.read(inputBuffer);
            data = new String(inputBuffer);
            System.out.println(data);
            Toast.makeText(context, "Text read", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "Text not read", Toast.LENGTH_SHORT).show();

        } finally {
            try {
                input.close();
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

This code doesnt work. It always hits the exception.

"/data/data/com.name.somethingiremoved/files/worldonelayout.txt (No such file or directory)".

This happens at the first CATCH. BTW my file is in the root directory: Documents\Eclipse\workspace\project\worldonelayout.txt. I can also see the file in the browser on the left

From what I have seen here and on other sites, it is something to do with the Context class being derived from the Activity? I don't want to have this code in the same class as my activity. Is there a way round this?

If you need anything more from me, let me know.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
maffo
  • 1,393
  • 4
  • 18
  • 35
  • What exception is being thrown? – Ollie C Apr 05 '12 at 18:41
  • @OllieC /data/data/com.name.somethingiremoved/files/worldonelayout.txt (No such file or directory). The first CATCH. BTW my file is ine the root directory: Documents\Eclipse\workspace\project\worldonelayout.txt. I can also see the file in the browser on the left – maffo Apr 05 '12 at 18:45
  • Can you use system.out.println in android? It's also a good idea to show your logcat errors – Stagleton Apr 05 '12 at 18:52

1 Answers1

1

The open file is looking for a file on the phone's file system, not on the computer's. Its telling you exactly where it expects to find it - on the phone under /data/data/com.name.somethingiremoved/files/worldonelayout.txt

HannahMitt
  • 1,010
  • 11
  • 14
  • This does make sence. Do you know how I go about adding this file to the phone. – maffo Apr 05 '12 at 19:07
  • mount an actual phone or for the emulator http://stackoverflow.com/questions/2808632/manually-put-files-to-android-emulator-sd-card – HannahMitt Apr 06 '12 at 18:39