简体   繁体   English

在项目文件夹中找不到文件

[英]File not found in project folder

I have a function that loads a config.properties file from the main folder of the project, however, it gives me a FileNotFoundException and I have no idea why. 我有一个从项目主文件夹加载config.properties文件的函数,但是,它给了我FileNotFoundException,我也不知道为什么。 My method looks like: 我的方法如下:

public static Configfile configuration() {
        String server = null;
        int dbport = 0;
        String dbuser = null;
        String dbpass = null;

        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream("config.properties");
            prop.load(input);

            server = prop.getProperty("server");
            dbport = Integer.parseInt(prop.getProperty("dbport"));
            dbuser = prop.getProperty("dbuser");
            dbpass = prop.getProperty("dbpassword");
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new Configfile(server, dbport, dbuser, dbpass);
    }

And my config.properties file is in: 我的config.properties文件位于:

E:\\Android\\NSomething5 E:\\ Android \\ NSomething5

What am I missing? 我想念什么?

EDIT: Stacktrace: 编辑:Stacktrace:

12-04 17:00:07.619: W/System.err(21368): java.io.FileNotFoundException: /config.properties: open failed: ENOENT (No such file or directory)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.IoBridge.open(IoBridge.java:416)
12-04 17:00:07.619: W/System.err(21368):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-04 17:00:07.619: W/System.err(21368):    at java.io.FileInputStream.<init>(FileInputStream.java:105)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.utils.DBOperations.configuration(DBOperations.java:122)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.utils.DBOperations.<init>(DBOperations.java:24)
12-04 17:00:07.619: W/System.err(21368):    at com.example.nsomething.Report.<init>(Report.java:20)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.Class.newInstanceImpl(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.Class.newInstance(Class.java:1319)
12-04 17:00:07.619: W/System.err(21368):    at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
12-04 17:00:07.619: W/System.err(21368):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 17:00:07.619: W/System.err(21368):    at android.os.Looper.loop(Looper.java:137)
12-04 17:00:07.619: W/System.err(21368):    at android.app.ActivityThread.main(ActivityThread.java:5279)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.reflect.Method.invokeNative(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at java.lang.reflect.Method.invoke(Method.java:511)
12-04 17:00:07.619: W/System.err(21368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-04 17:00:07.619: W/System.err(21368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-04 17:00:07.619: W/System.err(21368):    at dalvik.system.NativeStart.main(Native Method)
12-04 17:00:07.619: W/System.err(21368): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.Posix.open(Native Method)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-04 17:00:07.619: W/System.err(21368):    at libcore.io.IoBridge.open(IoBridge.java:400)
12-04 17:00:07.619: W/System.err(21368):    ... 20 more

FileInputStream loads the file from either the absolute path provides or relative to the 'current working directory'. FileInputStream从提供的绝对路径或相对于“当前工作目录”的位置加载文件。 Now absolute path is not relevant to your example. 现在,绝对路径与您的示例无关。 So FileInputStream searches for your file 'config.properties' in the 'current working directory'. 因此,FileInputStream在“当前工作目录”中搜索文件“ config.properties”。 This directory is normally the one from which you started the program. 此目录通常是您启动程序的目录。 If you are doing this on Android, place your config file in te assets folder and use the assets API functions to load the file as a stream. 如果您是在Android上执行此操作,请将您的配置文件放置在te assets文件夹中,然后使用assets API函数将文件作为流加载。 If this is a standalone java program then place the file on classpath and use the classloader.getResourceAsStream to load the file. 如果这是一个独立的Java程序,则将该文件放在classpath上,并使用classloader.getResourceAsStream加载该文件。 Finally if you know the absolute path to the file use it directly. 最后,如果您知道文件的绝对路径,请直接使用它。

E:\Android\NSomething5

That is no valid Android file system path. 这不是有效的Android文件系统路径。 That is a Windows path. 这是Windows路径。 The app on your device has nothing to do with files on your pc. 设备上的应用程序与PC上的文件无关。

Put your config file in the assets folder of your project. 将您的配置文件放在项目的资产文件夹中。 Then at runtime use assetsmanager to open an InputStream and read from it. 然后在运行时使用assetsmanager打开InputStream并从中读取。 For the rest youc can use the same code. 其余的您可以使用相同的代码。 Only opening InputStream is different. 仅打开InputStream是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM