0

I am trying to delete a file from storage however when I do it returns true as it's been deleted yet on next boot up reads out the file as if it still exists :/

    package com.example.Mazer.Utilities;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.io.*;

public class ObjectSaver {

    public static void writeObjectToFile(Context c, Object object, String filename) {

        ObjectOutputStream objectOut = null;
        try {
            FileOutputStream fileOut = c.getApplicationContext().openFileOutput(filename, Activity.MODE_WORLD_READABLE);
            objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(object);
            fileOut.getFD().sync();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOut != null) {
                try {
                    objectOut.close();
                } catch (IOException e) {
                    Log.d("GameActivity", "Can't close objectOut ObjectOutputStream");
                }
            }
        }
    }

    public static void deleteObjectFromFile(Context c, String filename) {
        c.deleteFile( filename);
        //NOPE

        c.getApplicationContext().deleteFile(filename);
        //NOPE

        String s = c.getFilesDir().getAbsolutePath() + "/" + filename;
        c.deleteFile(s);
        //NOPE

    }


    public static Object readObjectFromFile(Context c, String filename) {

        ObjectInputStream objectIn = null;
        Object object = null;
        try {
            FileInputStream fileIn = c.getApplicationContext().openFileInput(filename);
            objectIn = new ObjectInputStream(fileIn);
            object = objectIn.readObject();
        } catch (FileNotFoundException e) {
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectIn != null) {
                try {
                    objectIn.close();
                } catch (IOException e) {
                    // do nowt
                }
            }
        }

        return object;
    }

}

As you can see I have added a few out of the million approaches I have tried, I have even tried over-writing the file.

I read from the file like so:

maze = (Maze) ObjectSaver.readObjectFromFile(Splash.this, "currentMaze");

and... I save to the file like so..

ObjectSaver.writeObjectToFile(context, new Maze(this), "currentMaze");
Adam Pitt
  • 128
  • 10
  • Have you gone through [this](https://stackoverflow.com/a/45301986/4116560) link? Its works perfect. – vss Aug 01 '17 at 11:36

2 Answers2

0

This might help:

import java.io.File;


 public static void deleteObjectFromFile(Context c, String filename) {

      File file = new File(fileName);
      if (file.exists()) { 
         file.delete();
      } 

    }
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0
boolean deleted = false;

File file = new File(selectedFilePath);
if (file.exists())
deleted = file.delete();

where selectedFilePath is the path of the file you want to delete - for example: /sdcard/MyFolder/example.mp3

Sney19
  • 125
  • 1
  • 4