java转换文本文件到xlsx(自制缓冲区,无需先验文件行数)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                java转换文本文件到xlsx(自制缓冲区,无需先验文件行数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            【0】README
 
0.1)本文代碼利用了 Apache POI 框架 建立 java 到 xlsx 代碼的聯系;
 
0.2)本文自制緩沖區從文本文件中讀取數據讀取,無需先驗文件行數;
 
0.3)本文通過緩沖區大小創建數組,數組容量自動增加,該raw idea 來自于 tomcat 源碼中在容器關聯管道增加非基礎閥的處理方式,包括Session池容量的增加也是這種處理方式;
 
0.4)for 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) Attention) A1)本文的capacity設定為10,你可以設定其他值,視具體情況而定; A2)本文代碼 在某些地方吧 data設置為null, 是為了便于 jvm 回收其內存; // the core code begins.int capacity=10; // buffer size, of course you can specify other values of capacity. step1 double[][] data = new double[capacity][];int lineNum = 0;while((str=reader.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%capacity==0) { // step3double[][] datacopy = new double[lineNum+capacity][]; // 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 style="font-family: SimSun; background-color: rgb(255, 255, 255);">?</span> 【2】intro to Apache POI 1)for downloading poi lib , please visit?http://poi.apache.org/download.html,but you can also download the libs of mine Apache POI lib. 2)POI 將 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;} } 3)POI 將每一行抽象為Row對象,將每個單元格抽象為 Cell對象,這樣就可以定位到每個單元格了,部分代碼如下: XlsxDataModel model = XlsxDataModel.getInstance(sheetName); Row row = model.getSheet().createRow(0); Cell cell = row.createCell(0); cell.setCellValue(""); 4)POI還從Sheet對象中抽象出遍歷每行的迭代器,代碼如下(從xlsx讀取數據需要迭代器): // 獲取數據行 迭代器final Iterator<Row> readIterator() {Iterator<Row> 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(basedir+"iris_data.txt")));String str;// the core code begins.int capacity=10; // buffer size, of course you can specify other values of capacity. double[][] data = new double[capacity][];int lineNum = 0;while((str=reader.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%capacity==0) { double[][] datacopy = new double[lineNum+capacity][];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(basedir+"gmeans_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) {Iterator<Row> 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_index<row_length)) {col_index = 0;row = itr.next();Iterator<Cell> 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_index<col_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) {Iterator<Row> itr = readIterator(); // 獲得遍歷行 的迭代器Row row = null; // 行對象int index = 0;// 行索引int row_length = row_end-row_start+1; // 數據行數// 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();Iterator<Cell> 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 Iterator<Row> readIterator() {Iterator<Row> 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;} }
                            
                        
                        
                        【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) Attention) A1)本文的capacity設定為10,你可以設定其他值,視具體情況而定; A2)本文代碼 在某些地方吧 data設置為null, 是為了便于 jvm 回收其內存; // the core code begins.int capacity=10; // buffer size, of course you can specify other values of capacity. step1 double[][] data = new double[capacity][];int lineNum = 0;while((str=reader.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%capacity==0) { // step3double[][] datacopy = new double[lineNum+capacity][]; // 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 style="font-family: SimSun; background-color: rgb(255, 255, 255);">?</span> 【2】intro to Apache POI 1)for downloading poi lib , please visit?http://poi.apache.org/download.html,but you can also download the libs of mine Apache POI lib. 2)POI 將 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;} } 3)POI 將每一行抽象為Row對象,將每個單元格抽象為 Cell對象,這樣就可以定位到每個單元格了,部分代碼如下: XlsxDataModel model = XlsxDataModel.getInstance(sheetName); Row row = model.getSheet().createRow(0); Cell cell = row.createCell(0); cell.setCellValue(""); 4)POI還從Sheet對象中抽象出遍歷每行的迭代器,代碼如下(從xlsx讀取數據需要迭代器): // 獲取數據行 迭代器final Iterator<Row> readIterator() {Iterator<Row> 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(basedir+"iris_data.txt")));String str;// the core code begins.int capacity=10; // buffer size, of course you can specify other values of capacity. double[][] data = new double[capacity][];int lineNum = 0;while((str=reader.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%capacity==0) { double[][] datacopy = new double[lineNum+capacity][];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(basedir+"gmeans_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) {Iterator<Row> 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_index<row_length)) {col_index = 0;row = itr.next();Iterator<Cell> 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_index<col_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) {Iterator<Row> itr = readIterator(); // 獲得遍歷行 的迭代器Row row = null; // 行對象int index = 0;// 行索引int row_length = row_end-row_start+1; // 數據行數// 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();Iterator<Cell> 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 Iterator<Row> readIterator() {Iterator<Row> 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;} }
總結
以上是生活随笔為你收集整理的java转换文本文件到xlsx(自制缓冲区,无需先验文件行数)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 一键超频上5GHz一键超频有用吗
- 下一篇: 云电脑是什么天翼云电脑是什么
