Android file not loading

I have a feeling that the path to the assets file is wrong but i am not sure of the right path to use. I had double checked the text file are actually located in the assets What makes me think its the wrong file or directory is because of the error no such file or directory?

There is conditional code wrapped around the Question code which is used to store the different fields. The code specifies if there is equal to four elements in the array then populates the array list with data from the file. If this condition is not made then a log will show an error. The file are named

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

Here is the 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

Below is Source Code

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;

    }

Answers


if you you use this you'll be ensure that paths are correct.

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

Need Your Help

Catch Exception in Application Startup (VS.Net)

visual-studio-2008 exception

I'm getting a System.NullReferenceException when my application starts up (after a small login screen) which doesn't crash the entire app but prevents it from loading correctly.

Spring Security Remember Me not working

java spring spring-security

I have spent a day on it but do not understand why remember_me in SPring Security is not working. I am only using xml as config with no Java code: