fileutil.java

来自「使用WEBWORK,SPRING,HIBERNATE编写的简单的添加」· Java 代码 · 共 68 行

JAVA
68
字号
/**
 *文件功能: 
 */
package com.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @作者 徐建协
 * @日期 2008-3-22
 */
public class FileUtil {
	
	/*******************
	 * 获取文件的后缀名
	 * @param fileName
	 * @return
	 */
	public static String getExt(String fileName){
		String[] array=fileName.split("[.]");
		if(array.length>0){
			return array[array.length-1];
		}
		return null;
	}
	
	/************************8
	 * 自动创建目录
	 * @param dir
	 * @return
	 */
	public static boolean creatDirs(String dir) {
		boolean res = false;
		File file = new File(dir);
		try {
			if (!file.exists()) {
				file.mkdirs();
			}
			res = true;
		} catch (Exception ex) {

		}
		return res;
	}	
	
	public static void copyFile(File srcFile,String targetPath) throws Exception{
		FileInputStream fileIn = new FileInputStream(srcFile);
		FileOutputStream outStream =new FileOutputStream(targetPath);
		byte[] buffer = new byte[(int) srcFile.length()];
		int len = 0;
		len = fileIn.read(buffer);
		outStream.write(buffer);
		outStream.close();
		outStream.close();
	}
	public  static byte[] getBytes(File f) throws Exception {

		byte[] buffer = new byte[(int) f.length()];
		int len = 0;
		FileInputStream fileIn = new FileInputStream(f);
		len = fileIn.read(buffer);
		fileIn.close();
		return buffer;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?