「java解析trs」java解析zip文件
本篇文章给大家谈谈java解析trs,以及java解析zip文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、Java拆分txt文件的写法
- 2、java怎样读取html文件
- 3、java里面这句代码看不懂String [] numStrs=arrayStr.split("{1,}");
- 4、Java中缓冲数组读取文件怎样定位每次读取的位置
Java拆分txt文件的写法
以下一个拆分txt的Util类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileCutter {
/**
*
*sourceFile:源文件的路径
*targetDirectory:保存文件的目录(例:'C:\\')
*prefix:是分割后文件的前缀(例:'2015-09-09')
*size:是分隔后单一文件的大小单位是2kb的倍数,size传10,分割后单一文件就是20K。传100,文件就是2M一个。
*
**/
public static void cutToMoreFile(String sourceFile, String targetDirectory, String prefix, int size)
{
//加载源文件
File source = new File(sourceFile);
InputStream in = null;
OutputStream out = null;
int len = 0;
int fileIndex = 1;
//设置一次加载的大小
byte[] buffer = new byte[2048];
try
{
//把源文件读到InputStream中
in = new FileInputStream(source);
//循环
while(true)
{
//分割后的文件流
out = new FileOutputStream(targetDirectory + File.separator + prefix + fileIndex++ + ".txt");
for(int i = 0; i size; i++)
{
//如果文件读取完就退回方法。
if((len = in.read(buffer)) != -1)
{
//写入分割后的文件
out.write(buffer, 0, len);
}else
{
//执行finally内容后,退出方法
return;
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
//关系流
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
以上,详细请看注释
java怎样读取html文件
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=0;itrs.size();i++){
Element tr=trs.get(i);
Elements tds=tr.getElementsByTag("td");
int realColNum=0;
for(int j=0;jtds.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=0;irowspan;i++){
for(int j=0;jcolspan;j++){
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=0;icols.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文件的例子文档地址:
java里面这句代码看不懂String [] numStrs=arrayStr.split("{1,}");
这是正则表达式,表示至少有一个字符,符合这个条件就分割,而任何字符串都基本上都符合,比如abcde,a符合那个正则,b、c、d、e都符合,那么abcde就被分割成了一个字符串数组[a,b,c,d,e]
Java中缓冲数组读取文件怎样定位每次读取的位置
被读取的文件可以放在硬盘的任意位置。 只要你新建文件IO流对象的时候把文件的物理路径写对就行了。代码例子如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* file IO流读取并输出文件
* @author young
*
*/
public class FileIO {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:/workspace/one/src/filecode/FileIO.java");// 要读的文件路径
InputStreamReader isr = new InputStreamReader(fis);// 字符流
BufferedReader br = new BufferedReader(isr); // 缓冲
String line = null;
while ((line = br.readLine()) != null) {// 字符不等于空
System.out.println(line);// 一行一行地输出
}
br.close();// 关闭文件
}
}
关于java解析trs和java解析zip文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。