PDA

View Full Version : Emulator & Windowns: Has anyone been able to read files or directories?



ppmoore
03-30-2009, 01:43 PM
Hello again.

Another newbie question for the long suffering experts here...

I'm running the Android emulator with eclipse under WinXP.

I have the following code:


File filePath = new File( "file.txt");
String fileName = filePath.getName();
try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
}
catch( FileNotFoundException e1 ) {
System.out.println("Error: File not found");
}This throws the exception FileNotFoundException.

Under Linux this would be easy, but how to correctly specify file paths under Windows? I've copied the file to all directories:


the windows C: root directory
the eclipse project root directory
the eclipse project src directory
the eclipse project bin directory

and it cannot find the file. Can anyone help?

Thanks,
Paul

ickyfehmleh
03-30-2009, 03:32 PM
You need to push it to the emulator/dev phone. Think of the emulator as its own OS running in its own sandbox much like Parallels or VMware allows you to run MS-Windows from within Linux.

Check the DDMS perspective in Eclipse for an easy GUI wrapper around 'adb'. I forget the directory you want to push it to offhand, I think it's "/data/app-data/your.namespace/".

ppmoore
03-31-2009, 02:04 AM
ickyfehmleh (http://androidcommunity.com/forums/members/ickyfehmleh/),

Many thanks again.

That worked. I copied the file to /data/app-private, and specified this explicit path as follows:

String fileName = "file.txt";
String dirName = "/data/app-private";
File filePath = new File( dirName, fileName);Paul

ickyfehmleh
03-31-2009, 04:27 PM
There are methods on Context (http://d.android.com/reference/android/content/Context.html) that will return the proper directory name. Future releases of Android may move /data/app-private/ to something entirely different, so it's important to use the proper API and not hardcode directories.