2015-01-31 73 views
2

我有一個位於主項目目錄中的csv文件調用v1.csv。當我嘗試閱讀燃料時,Java似乎從未找到它。我已經使用opencsv和掃描儀解決方案將文件讀入2d數組,但兩種方法都不起作用。我應該如何讓java知道在哪裏可以找到文件?Java不知道在哪裏可以找到.csv文件,我應該在哪裏放置它?

掃描儀解決方案:

BufferedReader reader = new BufferedReader(new FileReader(
      "v1.csv")); 

Opencsv解決方案:

CSVReader reader = new CSVReader(new FileReader("v1.csv")); 

Opencsv備選:

CSVReader reader = new CSVReader(new FileReader("/Users/yilun/Desktop/v1.csv")); 

堆棧跟蹤:

01-31 10:08:35.671: W/System.err(16946): java.io.FileNotFoundException: /Users/yilun/Downloads/mapquest-android-sdk/samples: open failed: ENOENT (No such file or directory) 
01-31 10:08:35.671: W/System.err(16946): at libcore.io.IoBridge.open(IoBridge.java:409) 
01-31 10:08:35.671: W/System.err(16946): at java.io.FileInputStream.<init>(FileInputStream.java:78) 
01-31 10:08:35.671: W/System.err(16946): at java.io.FileInputStream.<init>(FileInputStream.java:105) 
01-31 10:08:35.671: W/System.err(16946): at java.io.FileReader.<init>(FileReader.java:66) 
01-31 10:08:35.671: W/System.err(16946): at com.mapquest.android.samples.MyLocationMap.onCreate(MyLocationMap.java:73) 
01-31 10:08:35.681: W/System.err(16946): at android.app.Activity.performCreate(Activity.java:6084) 
01-31 10:08:35.681: W/System.err(16946): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-31 10:08:35.681: W/System.err(16946): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181) 
01-31 10:08:35.681: W/System.err(16946): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283) 
01-31 10:08:35.681: W/System.err(16946): at android.app.ActivityThread.access$800(ActivityThread.java:144) 
01-31 10:08:35.681: W/System.err(16946): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
01-31 10:08:35.681: W/System.err(16946): at android.os.Handler.dispatchMessage(Handler.java:102) 
01-31 10:08:35.681: W/System.err(16946): at android.os.Looper.loop(Looper.java:136) 
01-31 10:08:35.681: W/System.err(16946): at android.app.ActivityThread.main(ActivityThread.java:5158) 
01-31 10:08:35.681: W/System.err(16946): at java.lang.reflect.Method.invokeNative(Native Method) 
01-31 10:08:35.681: W/System.err(16946): at java.lang.reflect.Method.invoke(Method.java:515) 
01-31 10:08:35.681: W/System.err(16946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 
01-31 10:08:35.681: W/System.err(16946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 
01-31 10:08:35.681: W/System.err(16946): at dalvik.system.NativeStart.main(Native Method) 
01-31 10:08:35.681: W/System.err(16946): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
01-31 10:08:35.681: W/System.err(16946): at libcore.io.Posix.open(Native Method) 
01-31 10:08:35.681: W/System.err(16946): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
01-31 10:08:35.681: W/System.err(16946): at libcore.io.IoBridge.open(IoBridge.java:393) 
01-31 10:08:35.681: W/System.err(16946): ... 18 more 

一切都不起作用。

+0

什麼是異常/錯誤消息? – bgoldst 2015-01-31 17:52:10

+0

瞭解[class loader](http://www.javaworld.com/article/2077260/learn-java/learn-java-the-basics-of-java-class-loaders.html?null)。 – 2015-01-31 17:52:22

+2

取決於你從哪裏運行,嘗試System.out.println(新文件(「v1.csv」)。getAbsolutePath())打印出它正在查找的位置的完整路徑 – BretC 2015-01-31 17:52:37

回答