简体   繁体   中英

remove black diamond with a question mark text from android sdcard file

I am creating a listview with json data.When user is offline I want to show the data in listview. I have stored the json data in android sdcard. And I retrive the data when user is offline and showed it in listview. The problem is, when I read the file from directory it's show's me black diamond with a question mark and stores it in array list. this type of " t *" my question is how to remove this type of string from Arraylist. Someone please help

Save the Data:

 public  void saveMyData()
{
    try {
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+"cachefile.txt"));
        out.writeObject(al.toString());
        Toast.makeText(getApplicationContext(), "Data Saved..",Toast.LENGTH_LONG).show();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is my retrieving code.

String fileContent = "";
    try {
        String currentLine;
        BufferedReader bufferedReader;
        FileInputStream fileInputStream = new FileInputStream(getFilesDir()+"cachefile.txt");
        bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream,"UTF-8"));


        while ((currentLine = bufferedReader.readLine()) != null) {
            fileContent += currentLine + '\n';
        }

        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();

        Log.d("FAILED","THOS IS NULL");
        fileContent = null;
    }
    Log.d("SUCESS","SUCESS BUDDYY"+fileContent);

Broadly speaking, the issue is that you're using an ObjectOutputStream to write your data to the file, but a BufferedReader to read it back in. The ObjectOutputStream is specifically designed to allow objects and native data types to be written to a stream in a way they can be read back in via an ObjectInputStream to reconstitute the objects in the same way. This encoding is not meant to be human readable and usually contains extra characters as field separators.

I don't know what al is, but since you're calling toString() before you write it, I can assume that you want actual strings in a file you can read. To do this, you probably want to use a PrintStream and the PrintStream.println() method instead of the ObjectOutputStream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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