DorkMonstuh
DorkMonstuh

Reputation: 851

Java Android not writing to and reading from file properly

I have a problem with writing and reading from file as the title suggests. As I am saving to internal storage I am not able to access the default path that my file has saved to. I have 2 hashmaps, one of which holds the current data that is to be written to the file and the other is to store data when I read in from the text file. I am doing this to check whether or not the file has been written to properly. I'm sorry if there are any initial faults with the code it will be tidied up later once I get this working, I am fairly new to working with Android and java.

FILENAME is a String which holds just the filename itself along with its extension which is .txt

@Override
protected void onPause() 
{
    super.onPause();
    File fp = new File(context.getFilesDir(), FILENAME);
    if(fp.exists()){
        Log.d("FILE", "EXISTS");
    } else
        Log.d("FILE", "DOES NOT EXISTS");

    Log.d("HOME", "pause called");
    // loop until entries are written to file   
    try{
        if(!dataBank.isEmpty()){
            FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
            Writer out = new OutputStreamWriter(fos);

            Iterator<Map.Entry<String, String>> i = dataBank.entrySet().iterator();

            while(i.hasNext()){
                String key = i.next().getKey();
                Log.d("WRITE", key + "," + dataBank.get(key));
                out.write(key + "," + dataBank.get(key) + "\n");
            }

            out.close();
            fos.close();
        }

        FileInputStream fis = context.openFileInput(FILENAME);

        BufferedReader bReader = new BufferedReader(new InputStreamReader(fis));
        String read = "";
        Log.d("FILE0", "READ");
        while((read = bReader.readLine()) != null){
            String[] dataArray = new String[2];
            dataArray = read.split(",");
            compare.put(dataArray[0], dataArray[1]);
        }
        bReader.close();
        fis.close();
        Log.d("FILE1", "READ");

    }catch(Exception e){
        Log.d("ERROR", "onPause!");
    }
}

From the Log.d messages this is what I get. The order I entered them in are John, Alice and then Bob.

logcat log

I have an EditText box which is where the user types a name and 2 buttons, one button takes you to selecting an image from the gallery and from that it gets the file path and stores it in the value associated with the key which is the name typed into the textbox. These are added during this method:

public void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode == RESULT_OK){
        old_User = username; 
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);          
        dataBank.put(old_User, selectedImagePath);
    } else {
        Toast.makeText(context, "FAILED TO GET FILEPATH...", duration).show();
    }
}

Thanks!

Upvotes: 0

Views: 69

Answers (1)

Ranjithkumar
Ranjithkumar

Reputation: 707

If you need consistent ordering, you can use LinkedHashMap (for insertion/access order). so you can order it.

Upvotes: 1

Related Questions