在 Android 设备上保存 XML 文件并读取它
我在 Acer Iconia Tab A 100 上存储和读取 XML 文件时遇到问题。
我遵循的步骤是:
1) 将选项卡插入我的系统。
2) 将 test.xml 文件从我的笔记本电脑复制到 Acer Iconia Tab/ SDCard/ mydir/test.xml
3) 尝试在 Android 代码中打开此文件,如下所示:
File testFile = null;
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"mydir");
if(dir.exists())
testFile = new File(dir, "test.xml");
if (testFile != null)
Log.d ("File length="+testFile.length()); // returns 0
当我执行上述代码时,它打印出长度为 0 .我尝试了以下其他组合,但没有成功:
File dir = new File("/data","mydir");
File dir = new File("/mnt/sdcard","mydir");
有人可以帮忙吗?
谢谢。 阿尼
I have issues with storing and reading an XML file on my Acer Iconia Tab A 100.
The steps I followed are:
1) Plug in the tab to my system.
2) Copy test.xml file from my laptop to Acer Iconia Tab/ SDCard/ mydir/test.xml
3) Try to open this file in Android code, as follows:
File testFile = null;
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"mydir");
if(dir.exists())
testFile = new File(dir, "test.xml");
if (testFile != null)
Log.d ("File length="+testFile.length()); // returns 0
When I execute the above code, it prints out the length as 0. I have tried other combination as follows, but in vain:
File dir = new File("/data","mydir");
File dir = new File("/mnt/sdcard","mydir");
Can someone please help?
Thanks.
Ani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件长度
返回此抽象路径名表示的文件的长度。如果此路径名表示目录,则返回值未指定。
退货:
此抽象路径名表示的文件的长度(以字节为单位),如果文件不存在则为 0L
因此,只需检查文件是否存在,
通过使用
File.exist();< /code>
另外,通过查看文件资源管理器 -> DDMS通过eclipse找到文件的确切路径。它还显示文件的大小。
File length
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist
So just check it, whether file exist or not,
By using
File.exist();
Also, By looking in File Explorer -> DDMS via eclipse find the exact path of the file. It also show size of the file.