2012-06-11 52 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