Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android Studio中加载文件_Java_Android_File - Fatal编程技术网

Java 在Android Studio中加载文件

Java 在Android Studio中加载文件,java,android,file,Java,Android,File,我尝试过这里建议的解决方案: 除其他外 我需要从活动类外部加载文件,并且这些值可以在其他各种类中使用 (旁注:我不一定要把文件放在assets文件夹中,它们可以放在任何地方,只要我能以某种方式加载它们) 基本上,它告诉我检查名为“app.iml”的文件以获得以下行: option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" 确实如此 之后,将文件添加到“资产”目录 然后,我尝试使用以下内容加载该文件: File fil

我尝试过这里建议的解决方案: 除其他外

我需要从活动类外部加载文件,并且这些值可以在其他各种类中使用

(旁注:我不一定要把文件放在assets文件夹中,它们可以放在任何地方,只要我能以某种方式加载它们)

基本上,它告诉我检查名为“app.iml”的文件以获得以下行:

option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"
确实如此

之后,将文件添加到“资产”目录

然后,我尝试使用以下内容加载该文件:

File file = new File("IDs.txt");

但我得到一个文件找不到异常

无论我把文件放在哪里,我都无法加载它们。
有什么建议吗?

答案已通过将文件复制到应用程序文件夹更新

如果文件f=新文件,则无法从资源中打开文件。您应该像这样打开资产:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");
InputStream is = null;
    if (!ids.exists()) {
        AssetManager manager = getAssets();
        try {
            is = manager.open("IDs.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (is != null)
        try{
            OutputStream outputStream = new FileOutputStream(ids);
            byte buffer[] = new byte[1024];
            int length = 0;

            while ((length = is.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
现在,您可以为类设置文件或文件路径:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");

要获取完整路径,请使用
ids.getAbsolutePath()

答案已通过将文件复制到应用程序文件夹更新

如果文件f=新文件,则无法从资源中打开文件。您应该像这样打开资产:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");
InputStream is = null;
    if (!ids.exists()) {
        AssetManager manager = getAssets();
        try {
            is = manager.open("IDs.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (is != null)
        try{
            OutputStream outputStream = new FileOutputStream(ids);
            byte buffer[] = new byte[1024];
            int length = 0;

            while ((length = is.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
现在,您可以为类设置文件或文件路径:

File ids = new File(getExternalFilesDir("txt") + File.separator + "IDs.txt");

要获取完整路径,请使用
ids.getAbsolutePath()

可能的重复项我不想写,只是读。不要打开主项目的.iml文件,而是打开主模块的.iml文件。例如,如果您的项目是“MyProject”,而您实际运行的是“app”,则打开“MyProject/app/app.iml”文件。希望这有帮助。您试过了吗?是的,我检查的是app.iml文件。@Sorban抱歉,我误解了您关于“将文件添加到“资产目录”的问题。可能是重复的,我不想写,只想读。不要打开主项目.iml文件,而是打开主模块的.iml文件。例如,如果您的项目是“MyProject”,而您实际运行的是“app”,则打开“MyProject/app/app.iml”文件。希望这有帮助。您尝试过这个吗?是的,我检查的是app.iml文件。@Sorban抱歉,我误解了您关于“将文件添加到“资产目录”的问题。谢谢,但我需要其他方法来使用该文件。问题是,我无法使用getAssets,因为我的类不是一个具有上下文的活动。您可以在第一次启动时将资产从MainAcivity复制到应用程序文件夹。几分钟后,我将把这段代码添加到我的回答中。我仍然无法使用MainActivity类之外的方法扫描文件,使用此解决方案…谢谢,但我需要其他方法来使用文件。问题是,我无法使用getAssets,因为我的类不是一个具有上下文的活动。您可以在第一次启动时将资产从MainAcivity复制到应用程序文件夹。几分钟后,我将把这段代码添加到我的回答中。我仍然无法使用MainActivity类之外的方法扫描文件,使用此解决方案。。。