为什么Eclipse说找不到我的.json文件?它位于我的主要项目之外:
StockIODebug /资产/ StockIODebug.json
我尝试将.json文件移动到不同的文件夹但没有运气。我开始认为除了找不到文件之外还有更多的东西。
public void clickme (View view) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("/assets/StockIODebug.json"));
//Read entire file as object
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
logcat的:
03-04 21:31:00.356: I/Timeline(24149): Timeline: Activity_idle id: android.os.BinderProxy@faa2ff9 time:78464377
03-04 21:31:01.106: D/ViewRootImpl(24149): ViewPostImeInputStage processPointer 0
03-04 21:31:01.156: D/ViewRootImpl(24149): ViewPostImeInputStage processPointer 1
03-04 21:31:01.166: W/System.err(24149): java.io.FileNotFoundException: /assets/StockIODebug.json: open failed: ENOENT (No such file or directory)
03-04 21:31:01.186: W/System.err(24149): at libcore.io.IoBridge.open(IoBridge.java:452)
03-04 21:31:01.186: W/System.err(24149): at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-04 21:31:01.186: W/System.err(24149): at java.io.FileInputStream.<init>(FileInputStream.java:103)
03-04 21:31:01.186: W/System.err(24149): at java.io.FileReader.<init>(FileReader.java:66)
03-04 21:31:01.186: W/System.err(24149): at com.example.stockiodebug.MainActivity.clickme(MainActivity.java:37)
03-04 21:31:01.186: W/System.err(24149): at java.lang.reflect.Method.invoke(Native Method)
03-04 21:31:01.186: W/System.err(24149): at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
03-04 21:31:01.186: W/System.err(24149): at android.view.View.performClick(View.java:5697)
03-04 21:31:01.186: W/System.err(24149): at android.widget.TextView.performClick(TextView.java:10815)
03-04 21:31:01.186: W/System.err(24149): at android.view.View$PerformClick.run(View.java:22526)
03-04 21:31:01.186: W/System.err(24149): at android.os.Handler.handleCallback(Handler.java:739)
03-04 21:31:01.186: W/System.err(24149): at android.os.Handler.dispatchMessage(Handler.java:95)
03-04 21:31:01.186: W/System.err(24149): at android.os.Looper.loop(Looper.java:158)
03-04 21:31:01.186: W/System.err(24149): at android.app.ActivityThread.main(ActivityThread.java:7229)
03-04 21:31:01.186: W/System.err(24149): at java.lang.reflect.Method.invoke(Native Method)
03-04 21:31:01.186: W/System.err(24149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
03-04 21:31:01.186: W/System.err(24149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
03-04 21:31:01.186: W/System.err(24149): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
03-04 21:31:01.186: W/System.err(24149): at libcore.io.Posix.open(Native Method)
03-04 21:31:01.186: W/System.err(24149): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-04 21:31:01.186: W/System.err(24149): at libcore.io.IoBridge.open(IoBridge.java:438)
03-04 21:31:01.186: W/System.err(24149): ... 16 more
答案 0 :(得分:0)
我无法确定(没有登录到您的计算机!!),但最可能的解释是文件真的不是&#34;那里&#34;。也就是说,它确实不在目录&#34; / assets&#34;中。请注意,这是文件系统根目录中目录的绝对路径名。
这样的问题通常会成为对路径名的含义的误解。我的猜测是你正在尝试在项目中加载资产文件。在这种情况下,您需要:
如果您的应用程序打算打包/分发供其他人使用,那么最后一种方法是推荐的方法。 (所有其他条件相同......)