0

On the creation of my class, I want to populate an array with data from an xml file called "techs.xml" in the src folder.

public class MainActivity extends Activity {
private static String username;

public ArrayList<Tech> techList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LogInDialog login = new LogInDialog();
    login.show(getFragmentManager(), null);

    FileInputStream myInputStream;

    File file = new File("src/techs.xml");
    File file1 = new File("C:\\Users\\Joe\\My Document\\Classes\\CIS 408 - Mobile Appl Dev\\CII\\src\\techs.xml");
    System.out.println(file.exists());
    System.out.println(file.getAbsolutePath());
    System.out.println(file1.exists());
    System.out.println(file1.getAbsolutePath());
    try {
        myInputStream = new FileInputStream(file);

        System.out.println("wat");
        techList = new ArrayList(TechParser.parse(myInputStream));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

The output in LogCat of the relevant area is this:

04-30 18:29:07.703: I/Process(7846): Sending signal. PID: 7846 SIG: 9
04-30 18:29:10.923: I/System.out(7921): false
04-30 18:29:10.923: I/System.out(7921): /src/techs.xml
04-30 18:29:10.933: I/System.out(7921): false
04-30 18:29:10.933: I/System.out(7921): /C:\Users\Joe\My Document\Classes\CIS 408 - Mobile Appl Dev\CII\src\techs.xml
04-30 18:29:10.933: W/System.err(7921): java.io.FileNotFoundException: /src/techs.xml: open failed: ENOENT (No such file or directory)
04-30 18:29:10.933: W/System.err(7921):     at libcore.io.IoBridge.open(IoBridge.java:406)
04-30 18:29:10.933: W/System.err(7921):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-30 18:29:10.933: W/System.err(7921):     at com.example.cii.MainActivity.onCreate(MainActivity.java:35)
04-30 18:29:10.933: W/System.err(7921):     at android.app.Activity.performCreate(Activity.java:4492)
04-30 18:29:10.933: W/System.err(7921):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-30 18:29:10.933: W/System.err(7921):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-30 18:29:10.933: W/System.err(7921):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-30 18:29:10.943: W/System.err(7921):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-30 18:29:10.943: W/System.err(7921):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-30 18:29:10.943: W/System.err(7921):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 18:29:10.943: W/System.err(7921):     at android.os.Looper.loop(Looper.java:137)
04-30 18:29:10.943: W/System.err(7921):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 18:29:10.943: W/System.err(7921):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 18:29:10.943: W/System.err(7921):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 18:29:10.943: W/System.err(7921):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 18:29:10.943: W/System.err(7921):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 18:29:10.943: W/System.err(7921):     at dalvik.system.NativeStart.main(Native Method)
04-30 18:29:10.943: W/System.err(7921): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-30 18:29:10.943: W/System.err(7921):     at libcore.io.Posix.open(Native Method)
04-30 18:29:10.943: W/System.err(7921):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-30 18:29:10.943: W/System.err(7921):     at libcore.io.IoBridge.open(IoBridge.java:390)

So the issue here seems to be that the file doesn't exist even though it is in that folder (/CII/src/techs.xml). I used the absolute version of the file (which I don't want to do in the final version) to test it out and it also returned false. Can't anyone give me any hints as to what the problem might be?

Rob
  • 26,989
  • 16
  • 82
  • 98
  • you can't reference an arbitrary file in the src folder. One option is to put your file inside of assets and then use the AssetManager to get an inputstream for it. The path `C:\Users\...` is also not going to work on android because it isn't going to look on your PC it will look only on the android device and it does not use drive letters such as `C:` – FoamyGuy Apr 30 '13 at 22:35
  • I ended my code to reflect the current state, I had made a few changes that I hadn't reflected before, my apologies. That would make sense except that I didn't add the / in /C:... , the system itself did. And even given that, /src/techs.xml would be correct wouldn't it? @Foamy, Oh, you can't? I was never aware of that. I'll look into AssetManager then. – user2337643 Apr 30 '13 at 22:36
  • @user2337643 You're right the `/src/techs.xml` is not correct. In your answer you already said that should be something else (`/CII/src/techs.xml`). It seems like you are missing the base path here. – Viktor Seifert May 02 '13 at 07:55

1 Answers1

0

The basic problem you have is that the current working directory is set to the root directory / (if you are a windows user you can think of this as c:\). So the file src/techs.xml is being interpreted as /src/techs.xml which doesn't exist.

I would suggest you use a resource instead. This will let you package an xml file in your source folder and access it at run time.

http://developer.android.com/guide/topics/resources/accessing-resources.html

If you would prefer to continue using a file then you will need to find the place android has put your app so that you know the absolute path to your file. For that you can see this question. Though you should remember that files in src will be packaged inside your jar:

Get Application Directory

For your understanding: any path starting with / is considered to be absolute. Any path not starting with / is considered relative to the current working directory. So if the current working directory is /foo then a path without a preceding / will be considered /foo/path. In your case the working directory is / so src/techs.xml becomes /src/techs.xml

Community
  • 1
  • 1
Philip Couling
  • 13,581
  • 5
  • 53
  • 85