---
title: "assert目录下文件的读取"
author: "AndLi"
date: "2012-03-30T08:20:12Z"
tags: []
categories: ["移动开发"]
slug: "assert"
draft: false
lastmod: "2012-03-30T08:23:31Z"
id: 1469543
url: "https://andli.iteye.com/blog/1469543"
---

**普通的打开方式是：**

**InputStream inputStream = getAssets().open("test.xml");**

**带文件夹的：**

**getAssets().open("database/db.db");**

**在webview中的打开是：webView.loadUrl("file:///android\_asset/test/index.html");**

 

 

// bs = FileHelp.FileToXML("history.xml", getCon());// 直接读取根目录下的xml文件

 

 

```
bs = FileHelp.FileToXML(getCon().getResources().getAssets().open(
					"test/datasync.xml"), getCon());// 直接读取xml
```

 

 

```
public static String FileToXML(String filename, Context context) {
		InputStream ios = context.getClassLoader()
				.getResourceAsStream(filename);// 读取XML流
		BufferedReader in = new BufferedReader(new InputStreamReader(ios));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		try {
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return buffer.toString();
	}
	
	public static String FileToXML(InputStream ios, Context context) {
		// InputStream ios = context.getClassLoader()
		// .getResourceAsStream(filename);// 读取XML流
		BufferedReader in = new BufferedReader(new InputStreamReader(ios));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		try {
			while ((line = in.readLine()) != null) {
				buffer.append(line);
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return buffer.toString();
	}
```