IOUtil

package netstar.ext.cradle.laptop.utils;

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.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class Utils {
	
	/**
	 * 备份文件
	 *	@param in
	 *	@param out
	 *调用:Utils.CopyFile(new File("C:\\xx.sql"), new File("C:\\copy.sql"));
	 */
	public static  void   CopyFile(File   in,   File   out)   throws   Exception   { 
        FileInputStream   fis     =   new   FileInputStream(in); 
        FileOutputStream   fos   =   new   FileOutputStream(out); 
        byte[]   buf   =   new   byte[1024]; 
        int   i   =   0; 
        while((i=fis.read(buf))!=-1)   { 
            fos.write(buf,   0,   i); 
            } 
        fis.close(); 
        fos.close(); 
        } 
    
	
	/**
	 * 创建文件
	 * @param filePath:文件路径
	 * @param directoryPath:文件所在文件夹路径
	 * 调用:Utils.createFile("D:\\data\\Putaway_Tasks.dat", "D:\\data");
	 */
	public static File createFile(String filePath, String directoryPath) {
		File file = new File(directoryPath);
		if (!file.exists()) {
			System.out.println(directoryPath + "目录不存在,创建该目录");
			file.mkdir();// 创建目录
		}
		file = new File(filePath);
		
		if(file.exists()){
			file.delete();
		}
		if (!file.exists()) {
			try {
				if (file.createNewFile())
					System.out.println(directoryPath + "该文件成功创建");
			} catch (IOException e) {
			}
		}
		return file;
	}

	/**
	 * 文件内容写入
	 * 
	 * @param aFile
	 * @param aContents
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void setContents(File aFile, String aContents)
			throws FileNotFoundException, IOException {
		if (aFile == null) {
			throw new IllegalArgumentException("File should not be null.");
		}
		if (!aFile.exists()) {
			throw new FileNotFoundException("File does not exist: " + aFile);
		}
		if (!aFile.isFile()) {
			throw new IllegalArgumentException("Should not be a directory: "
					+ aFile);
		}
		if (!aFile.canWrite()) {
			throw new IllegalArgumentException("File cannot be written: "
					+ aFile);
		}
		try {
			OutputStreamWriter writer = new OutputStreamWriter(
					new FileOutputStream(aFile, true), "UTF-8");
			BufferedWriter fbw = new BufferedWriter(writer);
			fbw.write(aContents);
			fbw.newLine();
			fbw.close();
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		}
	}

	/**
	 * 读取文件内容
	 * 
	 * @param fileName
	 * @return
	 */
	public static BufferedReader readFile(String fileName) {
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(fileName));
			return br;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 字符串转日期类型
	 * 
	 * @param strDate
	 * @return java.sql.Date
	 */
	public static java.sql.Date stringConvertDate(String strDate) {
		java.util.Date today = new java.util.Date();
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		try {
			today = formatter.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long t = today.getTime();
		java.sql.Date sqlDate = new java.sql.Date(t);
		return sqlDate;
	}
	
	public static void main(String[] args) {
		File in=new File("C:\\xx.sql");
		File out=new File("C:\\yy.sql");
		try {
			Utils.CopyFile(new File("C:\\xx.sql"), new File("C:\\yy.sql"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值