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();
		}
	}
}

### Hutool IoUtil 类 使用方法 Hutool 的 `IoUtil` 是一个非常实用的工具类,提供了大量用于简化输入输出操作的方法。这些方法能够帮助开发者更高效地处理文件读写、流复制等常见 I/O 操作。 #### 基本功能介绍 `IoUtil` 提供了许多静态方法来方便地执行常见的 IO 操作。例如: - 流之间的数据传输 - 文件读取与写入 - 字符串和字节数组间的转换 - 关闭资源对象 通过使用此类中的方法,可以减少编写重复代码的工作量并提高程序稳定性[^1]。 #### 示例代码展示 下面是一些具体的例子展示了如何利用 `IoUtil` 进行不同类型的IO操作: ##### 复制 InputStream 到 OutputStream ```java import cn.hutool.core.io.IoUtil; public class Example { public static void copyStream() throws Exception{ try (InputStream inputStream = new FileInputStream("source.txt"); OutputStream outputStream = new FileOutputStream("destination.txt")) { // Copy from input stream to output stream using IoUtil.copy() IoUtil.copy(inputStream, outputStream); } } } ``` ##### 将 File 转换为 byte 数组 ```java import java.io.File; import cn.hutool.core.io.IoUtil; public class Example { public static byte[] fileToBytes(String filePath){ File file = new File(filePath); // Convert a file into bytes array with readBytes method of IoUtil. return IoUtil.readBytes(file); } } ``` ##### 把字符串写入到指定路径下的新创建文件中 ```java import cn.hutool.core.io.IoUtil; public class Example { public static void writeStringToFile(){ String content = "Hello world!"; // Write string data directly to the specified path as a newly created file by IoUtil.writeUtf8(). IoUtil.writeUtf8(new File("/path/to/your/file"), true, content); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值