Alternative to java.nio.file

一曲冷凌霜 提交于 2019-12-10 16:23:45

问题


I'm using Android Studio 2.3.1 and jdk 1.8 for this project.

I import a project and it's using 'java.nio.file.Files' and 'java.nio.file.Paths'.

Package "java.nio.file" does not exist in Java 8.

Also Android Studio does not allow to select jdk 1.7.

What can i do with this code ?

public void toFile(final String path) throws IOException {
    Files.write(Paths.get(path), toBuffer());
}

and this code

public static Source fromFile(final String path) throws IOException {
    return fromBuffer(Files.readAllBytes(Paths.get(path)));
}

** (Cannot resolve Files and Paths)

Thanks


回答1:


Good news: java.nio.* in now part of Android O!

More information here https://developer.android.com/reference/java/nio/package-summary.html




回答2:


Android does not offer all classes that "conventional Java" has to offer. Files are 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.

I found this library that claims to be

NNIO: A Java NIO.2 Substitute Library

but I think you should use instead guava to implement the same functionality, as it is used in many first-class apps and has a big community.

Edit: As Android O Developer Preview java.nio.file is supported. https://developer.android.com/reference/java/nio/file/package-summary.html



来源:https://stackoverflow.com/questions/43706789/alternative-to-java-nio-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!