1

I create and manage file from android application with this code but what I want then its to delete it.

This is the code how I write and read it:

 private String readDataFromString()
{
    try{
        FileInputStream fis = this.openFileInput("encryptedNotePad.txt");
        InputStreamReader isr = new InputStreamReader(fis);

        char[] inputBuffer = new char[100];
        String s = "";

        int charRead;
        while((charRead = isr.read(inputBuffer)) > 0){
            // Convertimos los char a String
            String readString = String.copyValueOf(inputBuffer, 0, charRead);
            s += readString;

            inputBuffer = new char[100];
        }

        isr.close();
        return s;

    }catch (IOException ex){
        ex.printStackTrace();
    }
    return null;
}


private void writeDataToString (String data) throws FileNotFoundException {
    try{
        //FileOutputStream fos = openFileOutput("encryptedNotePad.txt", MODE_PRIVATE);
        FileOutputStream fos = this.openFileOutput("encryptedNotePad.txt", MODE_PRIVATE);
        OutputStreamWriter osw = new OutputStreamWriter(fos);

        // Escribimos el String en el archivo
        osw.write(data);
        osw.flush();
        osw.close();

    }catch (IOException ex){
        ex.printStackTrace();
    }
}

How Can I delete it?

I found this:

File file = new File(selectedFilePath);
boolean deleted = file.delete();

But, I dont know the file path.Whats the file path?

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Julián Oviedo
  • 564
  • 5
  • 19

1 Answers1

0

But, I dont know the file path.Whats the file path?

Path means the file's path where you can get access to it or doing stuff on it.

I believe in your case, it will be:

encryptedNotePad.txt

Like you used it already:

FileOutputStream fos = this.openFileOutput("encryptedNotePad.txt", MODE_PRIVATE);

So if you give the path to the following code, it should work fine I hope:

File file = new File("encryptedNotePad.txt");
boolean deleted = file.delete();
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thanks a lot. I use this code but the file isnt delete: File file = new File(file_name+".txt"); if(file.exists() && !file.isDirectory()) { // call delete method boolean success = file.delete(); if (success) { Toast.makeText(this, "File was deleted",Toast.LENGTH_LONG).show(); } } – Julián Oviedo Oct 09 '18 at 15:20
  • Any errors do you see? Perhaps the path which you're trying to delete that file doesn't have the right permission? – ʍѳђઽ૯ท Oct 09 '18 at 15:43
  • Hello. Thats the problem, I dont get any error message, it just pass without delete that. For that I set up that toast message to check this out. I set up READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission in manifest. But regarding the file path, I dont know what to do because I open it with "FileOutputStream fos = this.openFileOutput("encryptedNotePad.txt", MODE_PRIVATE); " and when I try to delete it with File file = new File("encryptedNotePad.txt"); boolean deleted = file.delete();, but deleted is false. There is another way to delete a file? or what Im doing wrong? – Julián Oviedo Oct 09 '18 at 19:29
  • You missed a parameter I guess. Check this link: https://stackoverflow.com/a/24857764/4409113 – ʍѳђઽ૯ท Oct 09 '18 at 19:58