java中对txt和excel的读取和写入

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

 

你可能感兴趣的:(java基础,知识点)