简体   繁体   English

Java Android无法正确写入文件和从文件读取

[英]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. 我有2个哈希图,其中一个保存要写入文件的当前数据,另一个保存当我从文本文件中读取数据时存储的数据。 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. 很抱歉,如果代码有任何初始错误,待我解决后,将在以后进行整理,对于使用Android和Java来说,我还是一个新手。

FILENAME is a String which holds just the filename itself along with its extension which is .txt FILENAME是一个字符串,仅保留文件名本身及其扩展名.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. Log.d消息中,这就是我得到的。 The order I entered them in are John , Alice and then Bob . 我输入的顺序是JohnAliceBob

logcat日志

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. 我有一个EditText框,用户可以在其中输入名称和2个按钮,一个按钮可让您从图库中选择图像,并从中获取文件路径并将其存储在与名称即键相关的值中在文本框中输入。 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! 谢谢!

If you need consistent ordering, you can use LinkedHashMap (for insertion/access order). 如果需要一致的顺序,则可以使用LinkedHashMap (用于插入/访问顺序)。 so you can order it. 所以你可以订购。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM