Android import java.nio.file.Files; cannot be resolved

26,380

Solution 1

Android does not offer all classes that "conventional java" has to offer. Files is one of the classes, that Android doesn't offer.

You can have a look at the classes available in Android here: http://developer.android.com/reference/classes.html

So unfortunately you have to use other functions / classes to implement the same functionality.

PS: The class is shown in your screenshot because you browse the classes of java installed on your PC, not those that are be available on the Android phone / tablet.

Update

The Files/FileSystem classes have become available starting with API version 26.

Solution 2

Apparently java.nio.file is coming with Android O. https://developer.android.com/reference/java/nio/file/package-summary.html

Solution 3

Have a Look of this Link Android support some classes of NIO packages not full classes. But if you need to fast-up your code you can also use apache-commons api for the same.

Share:
26,380

Related videos on Youtube

Jakob
Author by

Jakob

Updated on July 09, 2022

Comments

  • Jakob
    Jakob almost 2 years

    I am trying out the new Gmail API and the samples use the classes in the java.nio.file package, e.i. Files and FileSystems.

    These classes was introduced in Java jdk 1.7 for the record, and since I am running jdk 1.7.0_65 in my Android app I have no idea why Android Studio cannot find these classes.

    The imports are:

    import java.nio.file.FileSystems;
    import java.nio.file.Files;
    

    My build.gradle file of course tells the system to use v. 1.7 like this

    android {
        compileSdkVersion 19
        buildToolsVersion '20'
        ...
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }
    

    I am pointing to the right directory of the jdk:

    enter image description here

    The jdk is listed in the External Libraries section:

    enter image description here

    And if I browse through the Java files I can even find java.nio.file.Files and .FileSystems:

    enter image description here

    Now, what the **** is going on!? From my understanding I am doing everything right here, any suggestions?

    • Joshua Pinter
      Joshua Pinter over 9 years
      Man! I am having the exact same issue! What was your work around for doing what we should have been able to do with Files? Any good third-party libraries? Thanks!
    • The Original Android
      The Original Android over 9 years
      Thank you for your fine analysis! It's rare to see.
    • Sirop4ik
      Sirop4ik about 8 years
      ok, but it still issue... Who know which kind of library we should use instead of?
    • intrepidis
      intrepidis over 6 years
      Try using methods like java.io.File.mkdirs(), etc.
  • Jakob
    Jakob almost 10 years
    I see, thank you very much. Not what I really wanted to hear, but that definitely clears up the enigma. thanks
  • Sirop4ik
    Sirop4ik about 8 years
    ok, but it still issue... Who know which kind of library we should use instead of?
  • intrepidis
    intrepidis over 6 years
    java.nio.file.Files is now in the link you provided, added in API level 26.