外包公司网站,平台一直维护是不是要跑路了,企业网站排名提升软件,dedecms模板站【0】README0.1#xff09;本文代码利用了 Apache POI 框架 建立 java 到 xlsx 代码的联系#xff1b;0.2#xff09;本文自制缓冲区从文本文件中读取数据读取#xff0c;无需先验文件行数#xff1b;0.3#xff09;本文通过缓冲区大小创建数组#xff0c;数组容量自动增…【0】README0.1本文代码利用了 Apache POI 框架 建立 java 到 xlsx 代码的联系0.2本文自制缓冲区从文本文件中读取数据读取无需先验文件行数0.3本文通过缓冲区大小创建数组数组容量自动增加该raw idea 来自于 tomcat 源码中在容器关联管道增加非基础阀的处理方式包括Session池容量的增加也是这种处理方式0.4for complete source code, please visit https://github.com/pacosonTang/postgraduate-research/tree/master/DataProcess【1】如何自制缓冲区读取数据step1先建立一个缓冲大小确定capacity的二维数组指针data step2通过循环读取数据并填充二维数组 step3判断二维数组大小是否等于原定的缓冲大小capacity step3.1若等于则增大缓冲区大小 为 newcapacity并建立容量为 newcapacity 的二维数组指针datacopy将原来的data数组的内容copy或填充到datacopy并使得data指针的指向等于datacopy的指向 step3.2若不等于继续下一步循环 step4循环完毕后建立容量为 newcapacity 的二维数组指针datacopy将原来的data数组的内容copy或填充到datacopy并使得data指针的指向等于datacopy的指向bingo AttentionA1本文的capacity设定为10你可以设定其他值视具体情况而定 A2本文代码 在某些地方吧 data设置为null 是为了便于 jvm 回收其内存 // the core code begins.int capacity10; // buffer size, of course you can specify other values of capacity. step1 double[][] data new double[capacity][];int lineNum 0;while((strreader.readLine())!null) { // step2String[] array str.split(,);double[] temp new double[array.length-1];for (int i 0; i array.length-1; i) {temp[i] Double.valueOf(array[i]);}if(lineNum%capacity0) { // step3double[][] datacopy new double[lineNumcapacity][]; // step3.1for (int i 0; i data.length; i) {datacopy[i] data[i];}data null;data datacopy; } data[lineNum] temp; // step3.2 }double[][] datacopy new double[lineNum][]; // step4.for (int i 0; i datacopy.length; i) {datacopy[i] data[i];}data null;data datacopy;// the core code ends.span stylefont-family: SimSun; background-color: rgb(255, 255, 255); /span 【2】intro to Apache POI1for downloading poi lib , please visit http://poi.apache.org/download.htmlbut you can also download the libs of mine Apache POI lib.2POI 将 xlsx 文件抽象为 Workbook对象将xlsx文件中每张工作表抽象为 Sheet对象代码如下public class XlsxDataModel
{private Workbook workbook;private Sheet sheet;static XlsxDataModel model;private XlsxDataModel(String sheetName){this.workbook new XSSFWorkbook();;this.sheet workbook.createSheet(sheetName);}public static XlsxDataModel getInstance(String sheetName){if(model null){model new XlsxDataModel(sheetName);}return model;}public Workbook getWorkbook() {return workbook;}public Sheet getSheet() {return sheet;}
}3POI 将每一行抽象为Row对象将每个单元格抽象为 Cell对象这样就可以定位到每个单元格了部分代码如下XlsxDataModel model XlsxDataModel.getInstance(sheetName);
Row row model.getSheet().createRow(0);
Cell cell row.createCell(0);
cell.setCellValue();4POI还从Sheet对象中抽象出遍历每行的迭代器代码如下从xlsx读取数据需要迭代器// 获取数据行 迭代器final IteratorRow readIterator() {IteratorRow itr null;try {File excel new File(this.dataPath);FileInputStream fis new FileInputStream(excel);// 创建工作簿XSSFWorkbook book new XSSFWorkbook(fis);// 创建工作簿下的第一页纸张XSSFSheet sheet book.getSheetAt(0);// 纸张的迭代器用于遍历行itr sheet.iterator();// Iterating over Excel file in Java} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return itr;}【3】将【1】中的idea 同 POI 结合起来0本文重点讲解写数据写入到xlsx文件读数据从xlsx文件中读数据只是po出方法而已1目的从文本文件中读取数据并转化为xlsx文件格式step1从文本读取数据 step2将存储文本数据的二维数组写入到xlsx public class DataProcess { // source code for step1 begins.public static void main(String[] args) throws IOException {String basedir System.getProperty(user.dir) File.separator;BufferedReader reader new BufferedReader(new InputStreamReader(new FileInputStream(basediriris_data.txt)));String str;// the core code begins.int capacity10; // buffer size, of course you can specify other values of capacity. double[][] data new double[capacity][];int lineNum 0;while((strreader.readLine())!null) {String[] array str.split(,);double[] temp new double[array.length-1];for (int i 0; i array.length-1; i) {temp[i] Double.valueOf(array[i]);}if(lineNum%capacity0) { double[][] datacopy new double[lineNumcapacity][];for (int i 0; i data.length; i) {datacopy[i] data[i];}data null;data datacopy; } data[lineNum] temp; }double[][] datacopy new double[lineNum][];for (int i 0; i datacopy.length; i) {datacopy[i] data[i];}data null;data datacopy;// source code for step1 ends.DataWrite writer new DataWrite(basedirgmeans_irise1.xlsx);// source code for step2 begins.writer.writeArray(data, item, data[0].length, iris); // source code for step2 ends. highlight line.}
}
/*** author Rong Tang* version 1.0* since 20150911*/
public class DataWrite {private String filepath;private FileOutputStream out;public DataWrite(String filepath) {this.filepath filepath;}/*** param data is a double array storing data written into xlsx.* param colPrefix is a row tag.* param headColNum is column number.* param sheetName is the name of sheet.* throws IOException*/public void writeArray(double[][] data, String colPrefix, int headColNum, String sheetName)throws IOException {XlsxDataModel model XlsxDataModel.getInstance(sheetName);Row row model.getSheet().createRow(0);Cell cell row.createCell(0);cell.setCellValue();for (int i 1; i headColNum; i) {cell row.createCell(i);cell.setCellValue(i);} // build the head line overfor (int i 0; i data.length; i) {row model.getSheet().createRow(i 1);cell row.createCell(0);cell.setCellValue(colPrefix (i 1));for (int j 0; j data[i].length; j) {cell row.createCell(j 1);cell.setCellValue(data[i][j]);}}// write the cluster result(centroid vector) into xlsx overout new FileOutputStream(filepath);model.getWorkbook().write(out);out.flush();out.close();System.out.println(write filepath over);}/*** param data is a int array storing data written into xlsx.* param colPrefix is a row tag.* param headColNum is column number.* param sheetName is the name of sheet.* throws IOException*/public void writeArray(int[][] data, String colPrefix, int headColNum, String sheetName)throws IOException {XlsxDataModel model XlsxDataModel.getInstance(sheetName);Row row model.getSheet().createRow(0);Cell cell row.createCell(0);cell.setCellValue();for (int i 1; i headColNum; i) {cell row.createCell(i);cell.setCellValue(i);} // build the head line overfor (int i 0; i data.length; i) {row model.getSheet().createRow(i 1);cell row.createCell(0);cell.setCellValue(colPrefix (i 1));for (int j 0; j data[i].length; j) {cell row.createCell(j 1);cell.setCellValue(data[i][j]);}}// write the cluster result(centroid vector) into xlsx overout new FileOutputStream(filepath);model.getWorkbook().write(out);out.flush();out.close();System.out.println(write filepath over);}
}
2本文象征性的po 出 读数据方法/*** author Rong Tang* version 1.0* since 20150911*/
public class DataRead {private String dataPath;public DataRead(String dataPath) {this.dataPath dataPath;}/*** * param row_start is a startup row startup index for reading. * param col_start is a startup column index for reading.* param array is a double array storing the data read from some xlsx. */public final void readDataToArray(int row_start, int col_start, double[][] array) {IteratorRow itr readIterator(); // 获得遍历行 的迭代器Row row null; // 行对象int row_index 0;// 行索引int col_index 0;// 列索引int row_length array.length; // 数据行数int col_length array[0].length; // 数据列数// the first row is ommited for it stores column indexif (itr.hasNext()) {itr.next();}// 定位行指针到 row_startwhile(itr.hasNext()){row_index;if(row_index row_start) {row_index 0;break;}itr.next();}// 定位 over// other rows stores time series datawhile (itr.hasNext() (row_indexrow_length)) {col_index 0;row itr.next();IteratorCell cellIterator row.cellIterator(); // 遍历每行单元格的迭代器Cell cell null; // the first column is ommited for it stores row indexif(cellIterator.hasNext()) { cellIterator.next();}// 定位列指针到 col_startwhile(cellIterator.hasNext()) {col_index;if(col_index col_start) {col_index 0;break;}cellIterator.next();}// 定位 overwhile (cellIterator.hasNext() (col_indexcol_length)) {cell cellIterator.next();array[row_index][col_index] cell.getNumericCellValue();}// 一行数据读取完毕row_index; } // 数据行读取完毕}// read data from xlsx to arraypublic final void readDataToArray(int row_start, int row_end) {IteratorRow itr readIterator(); // 获得遍历行 的迭代器Row row null; // 行对象int index 0;// 行索引int row_length row_end-row_start1; // 数据行数// the first row is ommited for it stores column indexif (itr.hasNext()) {row itr.next();}// 定位行指针到 row_startwhile(itr.hasNext()){index;if(index row_start) {break;}row itr.next();}index - row_start;// other rows stores time series datawhile (itr.hasNext() (index!row_length)) {int j 0;row itr.next();IteratorCell cellIterator row.cellIterator(); // 遍历每行单元格的迭代器Cell cell null; // the first column is ommited for it stores row indexif(cellIterator.hasNext())cell cellIterator.next();while (cellIterator.hasNext()) {cell cellIterator.next();ClusterData.items[index][j] cell.getNumericCellValue(); }// 一行数据读取完毕index; } // 数据行读取完毕}// 获取数据行 迭代器final IteratorRow readIterator() {IteratorRow itr null;try {File excel new File(this.dataPath);FileInputStream fis new FileInputStream(excel);// 创建工作簿XSSFWorkbook book new XSSFWorkbook(fis);// 创建工作簿下的第一页纸张XSSFSheet sheet book.getSheetAt(0);// 纸张的迭代器用于遍历行itr sheet.iterator();// Iterating over Excel file in Java} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return itr;}
}