0

我感觉资产文件的路径是错误的,但我不确定要使用的正确路径。我仔细检查了文本文件实际上位于assets什么让我认为它错误的文件或目录是因为错误没有这样的文件或目录?

问题代码周围有条件代码,用于存储不同的字段。代码指定数组中是否有四个元素,然后用文件中的数据填充数组列表。如果不满足此条件,则日志将显示错误。该文件名为

  • 测验1.txt
  • 测验2.txt
  • 测验3.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;

    }
4

1 回答 1

0

如果你使用它,你将确保路径是正确的。

AssetManager assetManager = getAssets();
String quizes[] = assetManager.list("myquizfolder");
for(String quiz : quizes) {
    InputStrem in = assetManager.open("myquizfolder/" + quiz);
}
于 2012-06-11T11:55:02.480 回答