java中對txt和excel的讀取和寫入
阿新 • • 發佈:2018-12-16
txt工具類:
package com.rj.bd.xm; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; /** * @desc txt工具類 * @author ws * @time 2018-10-17 */ public class TxtTools { private File file;// 檔案 private int MAX = 100000;// 資料條數 private String codeFormat ="UTF-8";//編碼格式 /** * @desc 判斷檔案是否存在,不存在建立檔案 */ public boolean judge(File file){ String pathname = file.getPath(); if (file.isFile() && file.exists()) { System.out.println("檔案存在!"); return true; } else { try { if (file.isDirectory()) { System.out.println("資料夾存在!"); file.createNewFile();// 建立檔案 System.out.println("檔案建立成功!"); return true; } else { System.out.println("資料夾不存在!"); String newPathname = ""; String[] arr = pathname.split("/"); for (int i = 0; i < arr.length - 1; i++) { newPathname += arr[i] + "/"; } File dir = new File(newPathname); dir.mkdirs();// 建立資料夾 System.out.println("資料夾建立成功!"); file.createNewFile();// 建立檔案 System.out.println("檔案建立成功!"); return true; } } catch (IOException e) { e.printStackTrace(); } } return false; } public File getFile() { return file; } public void setFile(File file) { judge(file); this.file = file; } public String getCodeFormat() { return codeFormat; } public void setCodeFormat(String codeFormat) { this.codeFormat = codeFormat; } /** * @desc 預設10萬條資料 * @param file */ public TxtTools(File file) { super(); judge(file); this.file = file; } /** * @desc 上傳檔案並調整資料條數 * @param file * @param max */ public TxtTools(File file, int max) { super(); judge(file); this.file = file; MAX = max; } /** * @desc 讀取txt檔案 * @param file */ public void readTxt(File file) { try { FileInputStream inputStream = new FileInputStream(file);// 將file轉為流 InputStreamReader inputStreamReader = new InputStreamReader( inputStream, codeFormat);// 將當前流進行編碼設定 BufferedReader bufferedReader = new BufferedReader( inputStreamReader);// 將攜帶有資料格式的流,裝入到緩衝區內 String everyLine = ""; while ((everyLine = bufferedReader.readLine()) != null) { System.out.println(everyLine); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); System.out.println("------讀取完畢------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @desc 讀取txt檔案 */ public void readTxt() { try { FileInputStream inputStream = new FileInputStream(file);// 將file轉為流 InputStreamReader inputStreamReader = new InputStreamReader( inputStream, codeFormat);// 將當前流進行編碼設定 BufferedReader bufferedReader = new BufferedReader( inputStreamReader);// 將攜帶有資料格式的流,裝入到緩衝區內 String everyLine = ""; while ((everyLine = bufferedReader.readLine()) != null) { System.out.println(everyLine); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); System.out.println("------讀取完畢------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @desc 寫入txt檔案 * @param max * 寫入資料條數 */ public void writeTxt(int max) { try { // 建立一個寫入的流 FileOutputStream fileOutputStream = new FileOutputStream(file, false);// false執行一次,true執行兩次 // 對流編碼 Writer fileWrite = new OutputStreamWriter(fileOutputStream,codeFormat); // 將已經設定好編碼格式的輸入流加入到緩衝區中 BufferedWriter bufferedWriter = new BufferedWriter(fileWrite); // 開始寫入 for (int i = 1; i <= max; i++) { bufferedWriter.write(i + ""); bufferedWriter.flush(); bufferedWriter.newLine(); } bufferedWriter.close(); fileWrite.close(); fileOutputStream.close(); System.out.println("------寫入成功------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @desc 寫入txt檔案 */ public void writeTxt() { try { // 建立一個寫入的流 FileOutputStream fileOutputStream = new FileOutputStream(file, false);// false執行一次,true執行兩次 // 對流編碼 Writer fileWrite = new OutputStreamWriter(fileOutputStream, codeFormat); // 將已經設定好編碼格式的輸入流加入到緩衝區中 BufferedWriter bufferedWriter = new BufferedWriter(fileWrite); // 開始寫入 for (int i = 1; i <= MAX; i++) { bufferedWriter.write(i + ""); bufferedWriter.flush(); bufferedWriter.newLine(); } bufferedWriter.close(); fileWrite.close(); fileOutputStream.close(); System.out.println("------寫入成功------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @desc 開啟一個寫入或者讀取的執行緒 * @param mode (write 或 read) */ public void openThread(String mode) { if (mode.equals("write")) { new Thread(new Runnable() { @Override public void run() { writeTxt(MAX); } },"writeTxt").start(); } else if (mode.equals("read")) { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000);//停留1秒,再讀取 } catch (InterruptedException e) { e.printStackTrace(); } readTxt(file); } },"readTxt").start(); } else { System.out.println("請正確填入引數:write 或 read"); } } }
excel工具類:(需匯入jxl包)
package com.rj.bd.xm; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; /** * @Desc:Excel工具類 * @author:YJL * @time:2018-10-16 下午09:36:32 */ public class ExcelTools { private File file;//要操作的檔案 private double MAX = 10000.0; //每頁可寫入資料的最大值 private int sheets;//需要多少頁 private int date=100000;//要寫入的資料,預設十萬 /** * @param file */ public ExcelTools(File file) { super(); this.file = file; } /** * @Desc 讀取檔案 * @param file 要讀取的檔案 */ public void readExcel(File file) { try { // 建立輸入流,讀取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook類 Workbook wb = Workbook.getWorkbook(is); // Excel的頁籤數量 int sheet_size = wb.getNumberOfSheets(); for (int index = 0; index < sheet_size; index++) { // 每個頁籤建立一個Sheet物件 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回該頁的總行數 for (int i = 0; i < sheet.getRows(); i++) { // sheet.getColumns()返回該頁的總列數 for (int j = 0; j < sheet.getColumns(); j++) { String cellinfo = sheet.getCell(j, i).getContents(); System.out.println(cellinfo); } } System.out.println("------第"+ index +"頁------"); } System.out.println("------讀取完畢------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Desc 讀取檔案 */ public void readExcel() { try { // 建立輸入流,讀取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook類 Workbook wb = Workbook.getWorkbook(is); // Excel的頁籤數量 int sheet_size = wb.getNumberOfSheets(); for (int index = 0; index < sheet_size; index++) { // 每個頁籤建立一個Sheet物件 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回該頁的總行數 for (int i = 0; i < sheet.getRows(); i++) { // sheet.getColumns()返回該頁的總列數 for (int j = 0; j < sheet.getColumns(); j++) { String cellinfo = sheet.getCell(j, i).getContents(); System.out.println(cellinfo); } } System.out.println("------第"+ index +"頁------"); } System.out.println("------讀取完畢------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @Desc 檔案的寫入 * @param date 需要寫入的資料 * @param file 要寫入的檔案 * @throws IOException * @throws RowsExceededException * @throws WriteException */ public void writeExcel(int date,File file) throws IOException, RowsExceededException, WriteException{ sheetCount(date);//計算算出需要多少頁 WritableWorkbook workBook=Workbook.createWorkbook(file); int endDate = 0; int s = 0; for (int i = 1; i < sheets; i++) { WritableSheet sheet=workBook.createSheet("第"+i+"頁", i); for (int j = 0; j<MAX; j++) { Label label = new Label(0,j,String.valueOf(s++)); sheet.addCell(label); } } WritableSheet sheet=workBook.createSheet("第"+sheets+"頁", sheets); endDate = date-s; int r=0; while (s<date) { Label label = new Label(0,r++,String.valueOf(s++)); sheet.addCell(label); } System.out.println("成功將資料寫入到了"+sheets+"頁sheet表中,每頁最多"+MAX+"條資料,最後一頁有"+endDate+"條資料"); workBook.write(); workBook.close(); } /** * @Desc @Desc 檔案寫入 * @param date 要寫入的資料 * @throws IOException * @throws RowsExceededException * @throws WriteException */ public void writeExcel(int date) throws IOException, RowsExceededException, WriteException{ sheetCount( date);//計算算出需要多少頁 WritableWorkbook workBook=Workbook.createWorkbook(file); int endDate = 0; int s = 0; for (int i = 1; i < sheets; i++) { WritableSheet sheet=workBook.createSheet("第"+i+"頁", i); for (int j = 0; j<MAX; j++) { Label label = new Label(0,j,String.valueOf(s++)); sheet.addCell(label); } } WritableSheet sheet=workBook.createSheet("第"+sheets+"頁", sheets); endDate = date-s; int r=0; while (s<date) { Label label = new Label(0,r++,String.valueOf(s++)); sheet.addCell(label); } System.out.println("成功將資料寫入到了"+sheets+"頁sheet表中,每頁最多"+MAX+"條資料,最後一頁有"+endDate+"條資料"); workBook.write(); workBook.close(); } /** * @Desc 有參構造器 * @param file 要操作的檔案 * @param date 要寫入的資料 */ public ExcelTools(File file, int date) { super(); this.file = file; this.date = date; } /** * @Desc 檔案的寫入 * @throws IOException * @throws RowsExceededException * @throws WriteException */ public void writeExcel() throws IOException, RowsExceededException, WriteException{ sheetCount( date);//計算算出需要多少頁 WritableWorkbook workBook=Workbook.createWorkbook(file); int endDate = 0; int s = 0; for (int i = 1; i < sheets; i++) { WritableSheet sheet=workBook.createSheet("第"+i+"頁", i); for (int j = 0; j<MAX; j++) { Label label = new Label(0,j,String.valueOf(s++)); sheet.addCell(label); } } WritableSheet sheet=workBook.createSheet("第"+sheets+"頁", sheets); endDate = date-s; int r=0; while (s<date) { Label label = new Label(0,r++,String.valueOf(s++)); sheet.addCell(label); } System.out.println("成功將資料寫入到了"+sheets+"頁sheet表中,每頁最多"+MAX+"條資料,最後一頁有"+endDate+"條資料"); workBook.write(); workBook.close(); } /** * @Desc 檔案的寫入 * @param MAX 每頁可以寫入資料的最大值 * @param date 要寫入的資料 * @throws IOException * @throws RowsExceededException * @throws WriteException */ public void writeExcel(int MAX, int date) throws IOException, RowsExceededException, WriteException{ sheetCount( date); WritableWorkbook workBook=Workbook.createWorkbook(file); int endDate = 0; int s = 0; for (int i = 1; i < sheets; i++) { WritableSheet sheet=workBook.createSheet("第"+i+"頁", i); for (int j = 0; j<MAX; j++) { Label label = new Label(0,j,String.valueOf(s++)); sheet.addCell(label); } } WritableSheet sheet=workBook.createSheet("第"+sheets+"頁", sheets); endDate = date-s; int r=0; while (s<date) { Label label = new Label(0,r++,String.valueOf(s++)); sheet.addCell(label); } System.out.println("成功將資料寫入到了"+sheets+"頁sheet表中,每頁最多"+MAX+"條資料,最後一頁有"+endDate+"條資料"); workBook.write(); workBook.close(); } /** * @Desc 計算需要多少個sheet頁 * @param date * @return */ public int sheetCount(int date){ return sheets =(int) Math.round(date/MAX);//計算出需要多少sheet } /** * @desc 開啟一個寫入或者讀取的執行緒 * @param mode (write 或 read) */ public void openThread(String mode) { if (mode.equals("write")) { new Thread(new Runnable() { @Override public void run() { try { writeExcel(date); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } },"writeExcel").start(); } else if (mode.equals("read")) { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000);//停留1秒,再讀取 } catch (InterruptedException e) { e.printStackTrace(); } readExcel(file); } },"readExcel").start(); } else { System.out.println("請正確填入引數:write 或 read"); } } }