public class Utils {
    public static List<Message> getMessages() {
        //File file = new File("file:///android_asset/helloworld.txt");
        AssetManager assetManager = getAssets();
        InputStream ims = assetManager.open("helloworld.txt");    
     }
}

我正在使用这段代码试图从资产读取文件。我尝试了两种方法。首先,当使用文件时,我收到FileNotFoundException,当使用资产管理器getAssets()方法不被识别。 有什么解决办法吗?


当前回答

下面是一个在资产中读取文件的方法:

/**
 * Reads the text of an asset. Should not be run on the UI thread.
 * 
 * @param mgr
 *            The {@link AssetManager} obtained via {@link Context#getAssets()}
 * @param path
 *            The path to the asset.
 * @return The plain text of the asset
 */
public static String readAsset(AssetManager mgr, String path) {
    String contents = "";
    InputStream is = null;
    BufferedReader reader = null;
    try {
        is = mgr.open(path);
        reader = new BufferedReader(new InputStreamReader(is));
        contents = reader.readLine();
        String line = null;
        while ((line = reader.readLine()) != null) {
            contents += '\n' + line;
        }
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignored) {
            }
        }
    }
    return contents;
}

其他回答

getAssets()

它只适用于其他任何类的活动,你必须为它使用上下文。

让Utils类的构造函数将活动引用(丑陋的方式)或应用程序上下文作为参数传递给它。在Utils类中使用getAsset()。

以下是我在缓冲读取扩展/修改活动中所做的工作,以满足您的需求

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));

    // do reading, usually loop until end of file reading  
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

编辑:如果你的问题是关于如何在活动之外做这件事,我的回答可能是无用的。如果你的问题只是如何从资产读取文件,那么答案在上面。

更新:

要打开指定类型的文件,只需在InputStreamReader调用中添加类型,如下所示。

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

EDIT

正如@Stan在评论中所说,我给出的代码不是对行求和。mLine每次传递都会被替换。这就是为什么我写了//process行。我假设文件包含某种类型的数据(即联系人列表),每一行都应该单独处理。

如果你只是想在没有任何处理的情况下加载文件,你必须在每次传递时使用StringBuilder()对mLine求和并追加每个传递。

另一个编辑

根据@Vincent的评论,我添加了最后一个块。

还要注意,在Java 7及以上版本中,您可以使用try-with-resources来使用最新Java的AutoCloseable和Closeable特性。

上下文

@LunarWatcher在评论中指出getAssets()是上下文中的一个类。因此,如果你在活动之外调用它,你需要引用它并将上下文实例传递给活动。

ContextInstance.getAssets();

这在@Maneesh的回答中有解释。所以如果这对你有用的话,给他的答案投上一票因为是他指出了这一点。

读取和写入文件总是冗长且容易出错。避免这些答案,使用Okio即可:

public void readLines(File file) throws IOException {
  try (BufferedSource source = Okio.buffer(Okio.source(file))) {
    for (String line; (line = source.readUtf8Line()) != null; ) {
      if (line.contains("square")) {
        System.out.println(line);
      }
    }
  }
}

getAssets()方法将在Activity类内部调用时工作。

如果你在非Activity类中调用这个方法,那么你需要从Context中调用这个方法,这个Context是从Activity类中传递过来的。下面是可以访问该方法的行。

ContextInstance.getAssets();

ContextInstance可以作为Activity类的this传递。

@HpTerm答案Kotlin版本:

private fun getDataFromAssets(activity: Activity): String {

    var bufferedReader: BufferedReader? = null
    var data = ""

    try {
        bufferedReader = BufferedReader(
            InputStreamReader(
                activity?.assets?.open("Your_FILE.html"),
                "UTF-8"
            )
        )                  //use assets? directly if inside the activity

        var mLine:String? = bufferedReader.readLine()
        while (mLine != null) {
            data+= mLine
            mLine=bufferedReader.readLine()
        }

    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        try {
            bufferedReader?.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return data
}