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.printStack
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值