安卓导入web html 文件快速读写方法

html-css019

安卓导入web html 文件快速读写方法,第1张

总所周知在安卓的资源文件中可以直接放入文件,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()

}

美学批判历史与文学的本质性差异和同一性关联是一个古老的话题,亚里士多德在《诗学》中写道:“两者的差别在于一叙述已发生的事,一描述可能发生的事。因此,写诗这种活动比写历史更富于哲学意味,更被严肃地对待;因为诗所描述的事带有普遍性,历史则叙述个别的事。”[17] 《三国演义》的写作主体显然没有历史和文学的区分意识,所以,小说文本没有呈现历史与文学的差异性理念,所有历史和文学的故事元素和话语表达都是被混淆界线与纠缠不清的。正是导源于先天性质的美学观念的局限,小说被涂抹成为既不是历史又不符合文学一般规定性的一个混合性文本。问题的另一方面在于,历史和文学的同一性关联在哲学意义上,它们都应该和必然地表现历史的客观规律及其偶然性结构,换言之,历史和文学都必然共同地隐含着历史理性和辩证逻辑。[18]

     java读取html文件跟读取普通文件一样,都是使用输入输出流,但是java读取html文件之后还需要解析,使用Jsoup对html进行解析。下面是一个java读取带表格的任意html文件,并把html文件转换成excel的例子。

  要求: 要求能够实现给出任意带table表格的html文件,生成与表格相同内容的excel文件,附件可以作为测试文件,提供给定的roster.html文件,通过java代码,实现生成与html页面的table相同样式的roster.xls文件。

首先看roster.html:

java代码:

import java.io.BufferedReader

import java.io.File

import java.io.FileReader

import java.io.IOException

import jxl.Workbook

import jxl.write.Label

import jxl.write.WritableCellFormat

import jxl.write.WritableFont

import jxl.write.WritableSheet

import jxl.write.WritableWorkbook

import jxl.write.WriteException

import jxl.write.biff.RowsExceededException

import org.jsoup.Jsoup

import org.jsoup.nodes.Document

import org.jsoup.nodes.Element

import org.jsoup.select.Elements

public class HTMLTOExcel {

public static void main(String args[]) throws IOException{

///读取classpath目录下面的路径

String path=HTMLTOExcel.class.getResource("/").getPath()

path+="roster.html"

toExcel(path,"roster")      

    }

    //得到Document并且设置编码格式

public static Document getDoc(String fileName) throws IOException{

      File myFile=new File(fileName)

      Document doc= Jsoup.parse(myFile, "GBK","")

      return doc

}

///这个方法用于根据trs行数和sheet画出整个表格

public static void mergeColRow(Elements trs,WritableSheet sheet) throws RowsExceededException, WriteException{

int[][] rowhb=new int[300][50]

for(int i=0i<trs.size()i++){

 Element tr=trs.get(i)

 Elements tds=tr.getElementsByTag("td")

 

 int realColNum=0

 for(int j=0j<tds.size()j++){

  Element td=tds.get(j)  

  if(rowhb[i][realColNum]!=0){

  realColNum=getRealColNum(rowhb,i,realColNum)

  }

  int rowspan=1

  int colspan=1

  if(td.attr("rowspan")!=""){

  rowspan = Integer.parseInt(td.attr("rowspan"))

  }

  if(td.attr("colspan")!=""){

  colspan = Integer.parseInt(td.attr("colspan"))

  }

  String text=td.text()

  drawMegerCell(rowspan,colspan,sheet,realColNum,i,text,rowhb)

  realColNum=realColNum+colspan

 }

 

}

}

///这个方法用于根据样式画出单元格,并且根据rowpan和colspan合并单元格

public static void drawMegerCell(int rowspan,int colspan,WritableSheet sheet,int realColNum,int realRowNum,String text,int[][] rowhb) throws RowsExceededException, WriteException{

  for(int i=0i<rowspani++){

  for(int j=0j<colspanj++){

  if(i!=0||j!=0){

 text=""

  }

  Label label = new Label(realColNum+j,realRowNum+i,text)

   WritableFont countents = new WritableFont(WritableFont.TIMES,10) // 设置单元格内容,字号12  

   WritableCellFormat cellf = new WritableCellFormat(countents ) 

   cellf.setAlignment(jxl.format.Alignment.CENTRE)//把水平对齐方式指定为居中

   cellf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)//把垂直对齐方式指定为居

   label.setCellFormat(cellf)

   sheet.addCell(label)

   rowhb[realRowNum+i][realColNum+j]=1

  }

  }

  sheet.mergeCells(realColNum,realRowNum, realColNum+colspan-1,realRowNum+rowspan-1)

}

public static int getRealColNum(int[][] rowhb,int i,int realColNum){

while(rowhb[i][realColNum]!=0){

realColNum++

}

return realColNum

}

///根据colgroups设置表格的列宽

public static void setColWidth(Elements colgroups,WritableSheet sheet){

 if(colgroups.size()>0){

 Element colgroup=colgroups.get(0)

 Elements cols=colgroup.getElementsByTag("col")

 for(int i=0i<cols.size()i++){

 Element col=cols.get(i)

 String strwd=col.attr("width")

 if(col.attr("width")!=""){

 int wd=Integer.parseInt(strwd)

 sheet.setColumnView(i,wd/8)

 }

 

 }

 

 }

}

//toExcel是根据html文件地址生成对应的xls

public static void toExcel(String fileName,String excelName)throws IOException{

Document doc=getDoc(fileName)

    String title = doc.title()

    ///得到样式,以后可以根据正则表达式解析css,暂且没有找到cssparse

    Elements style= doc.getElementsByTag("style")

    ///得到Table,demo只演示输入一个table,以后可以用循环遍历tables集合输入所有table

    Elements tables= doc.getElementsByTag("TABLE")    

    if(tables.size()==0){

    return

    }

    Element table=tables.get(0)

   //得到所有行

    Elements trs = table.getElementsByTag("tr")

    ///得到列宽集合

    Elements colgroups=table.getElementsByTag("colgroup")

    

   try {

   //文件保存到classpath目录下面

    String path=HTMLTOExcel.class.getResource("/").getPath()

path+=excelName+".xls"

 System.out.println(path)

    WritableWorkbook book = Workbook.createWorkbook(new File(path))    

    WritableSheet sheet = book.createSheet("人事关系", 0)  

    setColWidth(colgroups,sheet)

    mergeColRow(trs,sheet)    

    book.write()    

    book.close()    

   } catch (RowsExceededException e) {

        e.printStackTrace()

   } catch (WriteException e) { 

        e.printStackTrace()

   }

}

}

解析html文件的例子文档地址:http://blog.csdn.net/androidwuyou/article/details/52636821