怎么读取html文件的内容

html-css09

怎么读取html文件的内容,第1张

读取html文件的内容方法如下:

1、找到.HTML文件的位置;

2、然后右击打开选择浏览器,就可以直接打开网页读取其中的内容。

总所周知在安卓的资源文件中可以直接放入文件,webView 加载的时候可以直接调用(webview.loadUrl("file:///android_asset/"+文件名称)),

但是assets 文件不支持写入文件,如果我们要更新html 文件怎么办呢。

1、我们可以把初始版本html 文件压缩到assets 下,然后在解压到本地文件中去。如果后面有小的更新我们只需要根据文件MD5去更新

2、如果是大的更新我们只需要重新导入压缩包到assets 下面打包即可。特别注意assets 文件压缩直接最好检查一下是否包含中文,不然会解压不成功

/**/**

* 解压缩功能.

* 将zipFile文件解压到folderPath目录下.

* ZipFile 解压全部文件效率高于 ZipInputStream

* @throws Exception

*/

public static int upZipFile(File zipFile, String folderPath, ISiteFileManagetEvent event)throws IOException {

ZipFile zfile =new ZipFile(zipFile)

    Enumeration zList = zfile.entries()

    ZipEntry ze =null

    byte[] buf =new byte[1024]

    int nowLength =0

    while (zList.hasMoreElements()) {

ze = (ZipEntry) zList.nextElement()

        if (ze.isDirectory()) {

Log.e("upZipFile", ze.getName())

            String dirstr = folderPath + ze.getName()

            dirstr =new String(dirstr.getBytes("8859_1"), "GB2312")

            File f =new File(dirstr)

            f.mkdir()

continue

        }

OutputStream os =new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())))

        InputStream is =new BufferedInputStream(zfile.getInputStream(ze))

        int readLen =0

        while ((readLen = is.read(buf, 0, 1024)) != -1) {

os.write(buf, 0, readLen)

        }

nowLength +=1

        event.SiteFileManagetProcess(nowLength, 201)

        is.close()

        os.close()

    }

event.SiteFileManagetFinishEvent("")

    if (zipFile !=null) {

boolean delete = zipFile.delete()

        Log.e("BaseFileUtil1", delete +"")

    }

zfile.close()

    return 0

}

/**

* 给定根目录,返回一个相对路径所对应的实际文件名.

*

* @param baseDir    指定根目录

* @param absFileName 相对路径名,来自于ZipEntry中的name

* @return java.io.File 实际的文件

*/

public static FilegetRealFileName(String baseDir, String absFileName) {

String[] dirs = absFileName.split("/")

    File ret =new File(baseDir)

    String substr =null

    if (dirs.length >1) {

for (int i =0i <dirs.length -1i++) {

substr = dirs[i]

            try {

//substr.trim()

                substr =new String(substr.getBytes("8859_1"), "GB2312")

            }catch (UnsupportedEncodingException e) {

e.printStackTrace()

            }

ret =new File(ret, substr)

        }

if (!ret.exists())

ret.mkdirs()

        substr = dirs[dirs.length -1]

        try {

substr =new String(substr.getBytes("8859_1"), "GB2312")

        }catch (UnsupportedEncodingException e) {

e.printStackTrace()

        }

ret =new File(ret, substr)

        return ret

    }

return ret

}

* 复制zip 压缩文件到 本地

* @param strOutFileName 本地文件路径

* @param zipName        assets 文件名称需要带文件后缀 xx.zip

* @throws IOException

*/

public static void copyBigDataToSD(Context context, String strOutFileName, String zipName)throws IOException {

InputStream myInput

    OutputStream myOutput =new FileOutputStream(strOutFileName)

    //操作assets 文件

    myInput = context.getAssets().open(zipName)

    byte[] buffer =new byte[1024]

    int length = myInput.read(buffer)

    while (length >0) {

myOutput.write(buffer, 0, length)

        length = myInput.read(buffer)

    }

myOutput.flush()

    myInput.close()

    myOutput.close()

}

java可以使用jsoup、htmlparser等工具进行html的读取和解析,以下是详细说明:

1、jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据。据说它是基于MIT协议发布的。

jsoup的主要功能如下:

从一个URL,文件或字符串中解析HTML;

使用DOM或CSS选择器来查找、取出数据;

可操作HTML元素、属性、文本;

示例代码:

Document doc = Jsoup.parse(input, "UTF-8", "http://www.dangdang.com")

Element content = doc.getElementById("content")

Elements links = content.getElementsByTag("a")

for (Element link : links) {

String linkHref = link.attr("href")

String linkText = link.text()

}

2、htmlparser是一个纯的java写的html解析的库,它不依赖于其它的java库文件,主要用于改造或提取html。它能超高速解析html,而且不会出错。现在htmlparser最新版本为2.0。 据说htmlparser就是目前最好的html解析和分析的工具。无论你是想抓取网页数据还是改造html的内容,用了htmlparser绝对会忍不住称赞。

在线文档: http://www.osctools.net/apidocs/apidoc?api=HTMLParser;http://htmlparser.sourceforge.net/project-info.html

示例代码:

Parser parser = new Parser ("http://www.dangdang.com")

NodeList list = parser.parse (null)

Node node = list.elementAt (0)

NodeList sublist = node.getChildren ()

System.out.println (sublist.size ())