0

I work on the project android application .. My application uses file.txt for the purposes of a process. I found an error opening trace file:

 No such file or directory (2) when running my application.

The manifest I have complete internet access permissions. I save the file.txt in res/raw/file.txt there is no error in the code line. I use a OS 4.1 (jelly bean) android"sdk" 21

Any help would be appreciated.

then I tried it once again by using a virtual android 4.2.2 ... but the results are also errors

public List<String> read() {
    List<String> ls = new ArrayList<String>();
    try {          
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("res/raw/katadasar.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        String strLine;
        //Read File Line By Line
        while ((strLine = bufferedReader.readLine()) != null) {
        // Print the content on the console
        // System.out.println(strLine);
        ls.add(strLine.trim().toLowerCase());
        }
        //Close the input stream
        inputStreamReader.close();
        } catch (Exception e) {//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        }
        return ls;
}

logcat

 07-24 03:38:38.710: D/dalvikvm(790): Not late-enabling CheckJNI (already on)
 07-24 03:38:39.060: I/dalvikvm(790): Turning on JNI app bug workarounds for target SDK     version 9...
 07-24 03:38:39.470: E/Trace(790): error opening trace file: No such file or directory (2)
 07-24 03:38:39.700: D/dalvikvm(790): GC_FOR_ALLOC freed 65K, 8% free 2403K/2608K,           paused 29ms, total 31ms
 07-24 03:38:39.720: I/dalvikvm-heap(790): Grow heap (frag case) to 3.944MB for 1536016-byte allocation
 07-24 03:38:39.770: D/dalvikvm(790): GC_FOR_ALLOC freed <1K, 6% free 3902K/4112K, paused 49ms, total 49ms
 07-24 03:38:39.820: D/dalvikvm(790): GC_CONCURRENT freed <1K, 6% free 3902K/4112K, paused 4ms+3ms, total 55ms
 07-24 03:38:40.120: D/(790): HostConnection::get() New Host Connection established 0x2a18c160, tid 790
 07-24 03:38:45.919: D/dalvikvm(790): GC_CONCURRENT freed 1505K, 38% free 2808K/4484K, paused 12ms+57ms, total 256ms
 07-24 03:38:54.389: D/dalvikvm(790): GC_CONCURRENT freed 121K, 31% free 3104K/4484K, paused 73ms+95ms, total 300ms
 07-24 03:38:58.909: D/dalvikvm(790): GC_CONCURRENT freed 269K, 28% free 3255K/4484K, paused 75ms+9ms, total 150ms
 07-24 03:38:58.909: D/dalvikvm(790): WAIT_FOR_CONCURRENT_GC blocked 69ms
 07-24 03:38:59.389: D/dalvikvm(790): GC_CONCURRENT freed 202K, 23% free 3477K/4484K, paused 75ms+8ms, total 138ms
 07-24 03:38:59.389: D/dalvikvm(790): WAIT_FOR_CONCURRENT_GC blocked 57ms
 07-24 03:38:59.669: D/dalvikvm(790): GC_CONCURRENT freed 575K, 24% free 3409K/4484K, paused 74ms+8ms, total 144ms
 07-24 03:38:59.679: D/dalvikvm(790): WAIT_FOR_CONCURRENT_GC blocked 33ms
 07-24 03:39:00.530: D/dalvikvm(790): GC_CONCURRENT freed 387K, 23% free 3492K/4484K, paused 29ms+89ms, total 223ms
4

1 回答 1

0

要在原始文件夹中打开文件,您可以尝试以下操作:

InputStream is = ctx.getResources().openRawResource(R.raw.katadasar);
InputStreamReader isr = new InputStreamReader(is);

此外,您应该更改您的 catch 中的代码:

catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());
}

为此,打印正确的错误:

catch (Exception e) {//Catch exception if any
    Log.e("YourTag", "Error:" +  e.getLocalizedMessage() );
}

尝试上面的代码(带有正确的错误logcat输出),如果它不起作用,请更新logcat输出以尝试定位错误

于 2013-07-24T05:18:28.283 回答