我试图从csv文件中获取值但获取ERROR 文件未找到。我是android编程新手。我尝试了CSVReader但得到了同样的错误。 I Tried these methods
try {
mng = getApplicationContext().getAssets();
test = mng.open("test.csv");
reader = new BufferedReader(newInputStreamReader(test));
for(L=0;((nextLine = reader.readLine()) != null);L++){
String[] cols = nextLine.split(",");
b[L]=cols[0];
m[L]=Integer.parseInt(b[L]);
}
Arrays.sort(m);
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Logcat错误。:
02-08 17:55:22.314: I/dalvikvm(2856): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-08 17:55:22.314: W/dalvikvm(2856): VFY: unable to resolve virtual method 411: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-08 17:55:22.314: D/dalvikvm(2856): VFY: replacing opcode 0x6e at 0x0002
02-08 17:55:22.314: I/dalvikvm(2856): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-08 17:55:22.314: W/dalvikvm(2856): VFY: unable to resolve virtual method 433: Landroid/content/res/TypedArray;.getType (I)I
02-08 17:55:22.314: D/dalvikvm(2856): VFY: replacing opcode 0x6e at 0x0002
02-08 17:55:22.354: I/dalvikvm(2856): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
02-08 17:55:22.354: W/dalvikvm(2856): VFY: unable to resolve virtual method 374: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-08 17:55:22.354: D/dalvikvm(2856): VFY: replacing opcode 0x6e at 0x0002
02-08 17:55:22.354: I/dalvikvm(2856): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
02-08 17:55:22.354: W/dalvikvm(2856): VFY: unable to resolve virtual method 376: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
02-08 17:55:22.354: D/dalvikvm(2856): VFY: replacing opcode 0x6e at 0x0002
02-08 17:55:22.444: D/libEGL(2856): loaded /vendor/lib/egl/libEGL_adreno.so
02-08 17:55:22.454: D/libEGL(2856): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
02-08 17:55:22.484: D/libEGL(2856): loaded /vendor/lib/egl/libGLESv2_adreno.so
02-08 17:55:22.484: I/Adreno-EGL(2856): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
02-08 17:55:22.484: I/Adreno-EGL(2856): OpenGL ES Shader Compiler Version: 17.01.10.SPL
02-08 17:55:22.484: I/Adreno-EGL(2856): Build Date: 11/04/13 Mon
02-08 17:55:22.484: I/Adreno-EGL(2856): Local Branch:
02-08 17:55:22.484: I/Adreno-EGL(2856): Remote Branch:
02-08 17:55:22.484: I/Adreno-EGL(2856): Local Patches:
02-08 17:55:22.484: I/Adreno-EGL(2856): Reconstruct Branch:
02-08 17:55:22.764: D/OpenGLRenderer(2856): Enabling debug mode 0
02-08 17:55:24.526: D/AbsolutePath(2856): /storage/emulated/0
02-08 17:55:24.526: W/System.err(2856): java.io.FileNotFoundException: /storage/emulated/0/null.csv: open failed: ENOENT (No such file or directory)
02-08 17:55:24.586: W/System.err(2856): at libcore.io.IoBridge.open(IoBridge.java:420)
02-08 17:55:24.586: W/System.err(2856): at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-08 17:55:24.586: W/System.err(2856): at java.io.FileReader.<init>(FileReader.java:42)
02-08 17:55:24.586: W/System.err(2856): at com.example.datapass.Myfirst$1.onClick(Myfirst.java:54)
02-08 17:55:24.586: W/System.err(2856): at android.view.View.performClick(View.java:4421)
02-08 17:55:24.586: W/System.err(2856): at android.view.View$PerformClick.run(View.java:17903)
02-08 17:55:24.586: W/System.err(2856): at android.os.Handler.handleCallback(Handler.java:730)
02-08 17:55:24.586: W/System.err(2856): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 17:55:24.586: W/System.err(2856): at android.os.Looper.loop(Looper.java:213)
02-08 17:55:24.586: W/System.err(2856): at android.app.ActivityThread.main(ActivityThread.java:5225)
02-08 17:55:24.586: W/System.err(2856): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 17:55:24.586: W/System.err(2856): at java.lang.reflect.Method.invoke(Method.java:525)
02-08 17:55:24.586: W/System.err(2856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-08 17:55:24.586: W/System.err(2856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-08 17:55:24.586: W/System.err(2856): at dalvik.system.NativeStart.main(Native Method)
02-08 17:55:24.596: W/System.err(2856): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-08 17:55:24.596: W/System.err(2856): at libcore.io.Posix.open(Native Method)
02-08 17:55:24.596: W/System.err(2856): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-08 17:55:24.596: W/System.err(2856): at libcore.io.IoBridge.open(IoBridge.java:404)
02-08 17:55:24.596: W/System.err(2856): ... 14 more
02-08 17:55:25.727: D/AndroidRuntime(2856): Shutting down VM
02-08 17:55:25.727: W/dalvikvm(2856): threadid=1: thread exiting with uncaught exception (group=0x415648b0)
02-08 17:55:25.727: E/AndroidRuntime(2856): FATAL EXCEPTION: main
02-08 17:55:25.727: E/AndroidRuntime(2856): java.lang.NullPointerException
02-08 17:55:25.727: E/AndroidRuntime(2856): at com.example.datapass.Mynew$1.onClick(Mynew.java:68)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.view.View.performClick(View.java:4421)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.view.View$PerformClick.run(View.java:17903)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.os.Handler.handleCallback(Handler.java:730)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.os.Looper.loop(Looper.java:213)
02-08 17:55:25.727: E/AndroidRuntime(2856): at android.app.ActivityThread.main(ActivityThread.java:5225)
02-08 17:55:25.727: E/AndroidRuntime(2856): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 17:55:25.727: E/AndroidRuntime(2856): at java.lang.reflect.Method.invoke(Method.java:525)
02-08 17:55:25.727: E/AndroidRuntime(2856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-08 17:55:25.727: E/AndroidRuntime(2856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-08 17:55:25.727: E/AndroidRuntime(2856): at dalvik.system.NativeStart.main(Native Method)
错误:&#34;打开失败:ENOENT(没有这样的文件或目录)&#34;
答案 0 :(得分:0)
要从内部存储中读取数据,您需要使用app文件夹并从中读取内容:
String yourFilePath = context.getFilesDir() + "/" + "hello.txt";
File yourFile = new File( yourFilePath );
您也可以使用这种方法:
FileInputStream fis = context.openFileInput("hello.txt", Context.MODE_PRIVATE);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}