【问题标题】:JAVA Android , acces to a file with getRessourceAsStreamJAVA Android , 使用 getResourceAsStream 访问文件
【发布时间】:2014-05-22 20:12:44
【问题描述】:

我想在我的 android 应用中处理一个属性文件。属性文件位于主文件夹中,与其他 jar 文件位于文件夹中。我知道,这不是最好的。

代码:

InputStream propStream=MainActivity.class.getResourceAsStream("data.properties");
         Log.d("propStreamOutput",propStream.toString());
                File data = new File(propStream.toString());


                Reader reader;
                try {
                    reader = new FileReader(data);
                     dataProp = new Properties();
                     dataProp.load( reader );
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                  catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

所以,第 2 行的日志说: libcore.net.url.JarURLConnectionImpl$JarURLConnectionInputStream@421302e8

以这种形式 logcat 调用 System.err :

java.io.FileNotFoundException/libcore.net.url.JarURLConnectionImpl$JarURLConnectionInputStream@42097218: open failed: ENOENT (No such file or directory) 

但是当我这样评论时:

        InputStream propStream=MainActivity.class.getResourceAsStream("data.properties");
         Log.d("propStreamOutput",propStream.toString());
                File data = new File(propStream.toString());


//                  Reader reader;
//                  try {
//                      reader = new FileReader(data);
//                       dataProp = new Properties();
//                       dataProp.load( reader );
//                  } catch (FileNotFoundException e) {
//                      // TODO Auto-generated catch block
//                      e.printStackTrace();
//                  }
//                  
//                    catch (IOException e) {
//                              // TODO Auto-generated  catch block
//                              e.printStackTrace();
//                          }

那么就没有 system.err 了!当然,第 2 行的日志调用也是如此。

【问题讨论】:

  • 您认为propStream.toString() 会做什么? (提示:快速调试会告诉你)

标签: java android inputstream


【解决方案1】:

如果你真的想使用 getResourceAsStream() 那么你必须把文件和你的 .java 一起放在 "src" 文件夹中 em> 文件。

这通常是标准 Java 应用程序的方式,但在 Android 上,执行此类操作的最常用方式是将文件放在“资产”文件夹中,通过 AssetsManager,可从您的 Activity 或任何Context。如下图:

InputStream is = null;
try {
   is = context.getAssets().open("data.properties");
} catch (final IOException e) {
   e.printStackTrace();
} finally {
   closeStreamGracefully(is);
}

【讨论】:

    【解决方案2】:

    getRessourceAsStream 返回一个 InputStream。当您对此调用 toString 时,它将返回一个string representation of the object。然后,当您创建数据文件并尝试读取它时,它将尝试打开像 @sdqzeffdfsf 这样的文件。如果您想从此文件中读取属性,请使用Properties

    Properties properties = new Properties();
    properties.load(propStream);
    

    这是标准的java。但是在android中你通常使用AssetManager

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 2018-09-16
      • 2018-11-10
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2023-03-29
      相关资源
      最近更新 更多