0

I want to create a txt file in the "Ocenki" folder

I wrote a code that should be written to the stringbuffer file:

try {
    FileOutputStream write =  new FileOutputStream(
        new File(
            context.getFilesDir() 
            + File.separator + "Ocenki" 
            + File.separator + url + ".txt")
    );

    String temp_write = stringBuffer.toString();

    write.write(temp_write.getBytes());
    write.close();
} catch (FileNotFoundException p) {
    p.printStackTrace();
} catch (IOException a) {
    a.printStackTrace();
}

Method context.getFilesDir() returns: "/data/user/0/com.example.kos/files"

Variable url contains: "2019 - 2020"

Error occurs in the code:

FileNotFoundException  
"java.io.FileNotFoundException: /data/user/0/com.example.kos/files/2019 - 2020.txt  
(No such file or directory)"

What do I do wrong?

UPD:

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kos"
    android:versionCode="2"
    android:versionName="1.1.1">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    ...

</manifest>

Here is a request for permissions:

 if(ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},3);
            if(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
               ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},3);
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
  • 1
    Have you added Read Write storage permission? – Nik Jan 03 '20 at 09:54
  • "FileNotFoundException "java.io.FileNotFoundException: /data/user/0/com.example.kos/files/url (No such file or directory)", Is this exactly copied from the trace. I think the stack trace should have "/data/user/0/com.example.kos/files/Ocenki/2019 - 2020.txt" – Aman jangra Jan 03 '20 at 09:56
  • If `context.getFilesDir()` returns `"/data/user/0/com.example.kos/files"`, `url` is a variable and its value is `"2019 - 2020"`, then why does your code try to access the (sub)directory `"/data/user/0/com.example.kos/files/url"`? Does it exist? Don't you want `"/data/user/0/com.example.kos/files/2019 - 2020"` instead? – deHaar Jan 03 '20 at 10:02
  • what your txt file name ? – Kanzariya Hitesh Jan 03 '20 at 10:12
  • Try changing value of `url` from "2019 - 2020" to "2019-2020". Remove spaces. – Rohit5k2 Jan 03 '20 at 10:15
  • Nik, yes; Aman jangra, deHaar I corrected the error text: Kanzariya Hitesh "2019 - 2020.txt" – koskokos_231 Jan 03 '20 at 10:16
  • Rohit5k2, It didn't help – koskokos_231 Jan 03 '20 at 10:20
  • You should create the file if it doesn't exist. https://stackoverflow.com/a/24676655/2936153 – Rohit5k2 Jan 03 '20 at 10:35
  • @Rohit5k2 Maybe not. The text file should have been created using the same code. Issue is with the path, maybe one of the directory from the path is missing. Is the Ocenki folder present ? – Aman jangra Jan 03 '20 at 10:56
  • 1
    Rohit5k2, Thank you, it solved my problem – koskokos_231 Jan 03 '20 at 11:02

1 Answers1

0

Here you go,

try{

  System.out.println("File Path = " + MainApplication.getFileDirectory() + "/Ocenki" + "/" + fileName);
  File fileCreated = new File(MainApplication.getFileDirectory() + "/Ocenki" + "/" + fileName);
  if (fileCreated.exists()) {
    fileCreated.delete();
  }
  outputStream = new FileOutputStream(MainApplication.getFileDirectory() + "/Ocenki" + "/" + fileName);
  byte[] buffer = new byte[1024];
  int read;

  while ((read = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, read);
  }

  //inputStream.close();
  outputStream.flush();
  outputStream.close();
  }catch (IOException io){
  io.printStackTrace();
}



public static File getFileDirectory() {
 return filesDir;
}

As I used this class in application class you can use this method on any class based on it's context

 filesDir = MainApplication.this.getFilesDir();
Ankit Tale
  • 1,924
  • 4
  • 17
  • 30