2012-06-11 94 views
0

我有一種感覺,資產文件的路徑是錯誤的,但我不確定正確的路徑使用。我有雙重檢查的文本文件實際上位於assets是什麼讓我覺得它的錯誤文件或目錄是因爲錯誤沒有這樣的文件或目錄?Android文件未加載

在用於存儲不同字段的問題代碼周圍有條件代碼。該代碼指定數組中是否等於四個元素,然後使用該文件中的數據填充數組列表。如果沒有這個條件,那麼日誌會顯示錯誤。該文件被命名爲

  • quiz1.txt
  • quiz2.txt
  • quiz3.txt

這裏是logcat的

06-11 11:29:02.526: I/global(317): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
06-11 11:29:02.526: E/path of text file(317): What is the output of: 
06-11 11:29:02.526: E/path of text file(317): echo 6 % 4;<.> 
06-11 11:29:02.526: E/path of text file(317): 2<.> 
06-11 11:29:02.526: E/path of text file(317): 4<.> 
06-11 11:29:02.526: E/path of text file(317): 2<.> 
06-11 11:29:02.526: E/path of text file(317): 0<.> 
06-11 11:29:02.526: W/System.err(317): Error: read() /What is the output of: 
06-11 11:29:02.526: W/System.err(317): echo 6 % 4;<.> 
06-11 11:29:02.526: W/System.err(317): 2<.> 
06-11 11:29:02.526: W/System.err(317): 4<.> 
06-11 11:29:02.526: W/System.err(317): 2<.> 
06-11 11:29:02.536: W/System.err(317): 0<.> 
06-11 11:29:02.536: W/System.err(317): (No such file or directory) 
06-11 11:29:02.536: E/YourApp(317): 1 
06-11 11:29:02.536: E/YourApp(317): Data was not split from file correctly 
06-11 11:29:02.536: I/global(317): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
06-11 11:29:02.536: E/path of text file(317): Which of following is NOT a valid comment in PHP<.> 
06-11 11:29:02.536: E/path of text file(317): 3<.> 
06-11 11:29:02.536: E/path of text file(317): // comment<.> 
06-11 11:29:02.536: E/path of text file(317): # comment<.> 
06-11 11:29:02.536: E/path of text file(317): <!-- comment --><.> 
06-11 11:29:02.536: W/System.err(317): Error: read() /Which of following is NOT a valid comment in PHP<.> 
06-11 11:29:02.536: W/System.err(317): 3<.> 
06-11 11:29:02.536: W/System.err(317):/comment<.> 
06-11 11:29:02.536: W/System.err(317): # comment<.> 
06-11 11:29:02.536: W/System.err(317): <!-- comment --><.> 
06-11 11:29:02.536: W/System.err(317): (No such file or directory) 
06-11 11:29:02.536: E/YourApp(317): 1 
06-11 11:29:02.536: E/YourApp(317): Data was not split from file correctly 
06-11 11:29:02.546: I/global(317): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 
06-11 11:29:02.546: E/path of text file(317): Which of following is a valid function definition in PHP<.> 
06-11 11:29:02.546: E/path of text file(317): 3<.> 
06-11 11:29:02.546: E/path of text file(317): int f()<.> 
06-11 11:29:02.546: E/path of text file(317): private int f()<.> 
06-11 11:29:02.546: E/path of text file(317): function f()<.> 
06-11 11:29:02.546: W/System.err(317): Error: read() /Which of following is a valid function definition in PHP<.> 
06-11 11:29:02.546: W/System.err(317): 3<.> 
06-11 11:29:02.546: W/System.err(317): int f()<.> 
06-11 11:29:02.546: W/System.err(317): private int f()<.> 
06-11 11:29:02.546: W/System.err(317): function f()<.> 
06-11 11:29:02.546: W/System.err(317): (No such file or directory) 
06-11 11:29:02.546: E/YourApp(317): 1 
06-11 11:29:02.546: E/YourApp(317): Data was not split from file correctly 

下面是源代碼

class Question 
{ 
    String question; 
    int answer; 
    String choice1; 
    String choice2; 
    String choice3; 

} 

public class Uploader extends Activity 
{ 
    private static final String LOGS = null; 
    Question q; 
    String textfile; 
    InputStream is; 

    public void onCreate(Bundle b) 
    { 
     super.onCreate(b); 
     setContentView(R.layout.basic); 

     ArrayList<Question> list = new ArrayList<Question>(); 

     for (int i = 1; i <= 3; i++) 
     { 

      try { 
      AssetManager assetManager = getAssets(); 
      // is = getResources().getAssets().open("quiz"+i+".txt"); 
      // 
     String quizes[] = assetManager.list("myquizfolder"); 
     for(String quiz : quizes) { 
      InputStream in = assetManager.open("myquizfolder/" + quiz); 
     textfile = convertStreamToString(in); 
     } 

       catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       Log.e("path of text file", textfile); 
       e.printStackTrace(); 
      } 

      ResultsReader reader = new ResultsReader(textfile); 
      reader.read(); 

      String str = ((ResultsReader) reader).getInput(); 
      String data[] = str.split("<.>"); 

      String datainfo = Integer.toString(data.length); 
      Log.e("YourApp", datainfo); 
      Question q = new Question(); 
      if (data.length == 4) 
      { 
       q.question = data[0]; 

       q.answer = Integer.parseInt(data[1]); 

       q.choice1 = data[2]; 
       q.choice2 = data[3]; 
       q.choice3 = data[4]; 
      } else 
      { 
       Log.e("YourApp", "Data was not split from file correctly"); 
      } 
      list.add(q); 

     } 
    } 
} 





public static String convertStreamToString(InputStream is) 
      throws IOException { 
      Writer writer = new StringWriter(); 

      char[] buffer = new char[2048]; 
      try { 
       Reader reader = new BufferedReader(new InputStreamReader(is, 
         "UTF-8")); 
       int n; 
       while ((n = reader.read(buffer)) != -1) { 
        writer.write(buffer, 0, n); 
       } 
      } finally { 
       is.close(); 
      } 
      String text = writer.toString(); 
      return text; 

    } 
+0

感謝編輯是有辦法,我可以分裂我的代碼和日誌所以分開 – alex

+0

顯示convertStreamToString功能 –

回答

0

如果你使用這個,你會確保路徑是正確的。

AssetManager assetManager = getAssets(); 
String quizes[] = assetManager.list("myquizfolder"); 
for(String quiz : quizes) { 
    InputStrem in = assetManager.open("myquizfolder/" + quiz); 
} 
+0

的代碼我已經更新了變化 – alex